Skip to content

Commit

Permalink
Merge pull request #53 from NuiCpp/devel
Browse files Browse the repository at this point in the history
Resizeable utility completed.
  • Loading branch information
5cript authored Aug 19, 2023
2 parents 1fed7bc + db17bb3 commit f0ddf68
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 46 deletions.
14 changes: 14 additions & 0 deletions nui/include/nui/frontend/extensions/make_resizeable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <nui/frontend/val.hpp>

namespace Nui
{
enum class ResizeableEdge
{
Right,
Bottom
};

void makeResizeable(Nui::val const& element, ResizeableEdge edge = ResizeableEdge::Right);
}
46 changes: 0 additions & 46 deletions nui/js/make_resizeable.js

This file was deleted.

61 changes: 61 additions & 0 deletions nui/js/make_resizeable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const makeResizeable = (element: HTMLElement, borderSize: number, edge?: "right" | "bottom") => {
edge = edge || "right";

let pos: number;
const resizer = (() => {
if (edge === "right") {
return (x: number, y: number, w: number, h: number) => {
const dx = -(pos - x);
pos = x;
element.style.width = (w + dx) + "px";
};
}
else if (edge === "bottom") {
return (x: number, y: number, w: number, h: number) => {
const dy = -(pos - y);
pos = y;
element.style.height = (h + dy) + "px";
};
}
else {
throw new Error("Invalid edge");
}
})();

const resize = (e: MouseEvent) => {
const w = parseInt(getComputedStyle(element, '').width);
const h = parseInt(getComputedStyle(element, '').height);

resizer(e.x, e.y, w, h);
}

element.addEventListener("mousedown", (e: MouseEvent) => {
if (!(e.target instanceof Element))
return;

const w = parseInt(getComputedStyle(e.target as Element, '').width);
const h = parseInt(getComputedStyle(e.target as Element, '').height);

if (edge == "right") {
if (w - e.x < borderSize + 1 /*rounding tolerance*/) {
pos = e.x;
document.addEventListener("mousemove", resize, false);
}
}
else if (edge == "bottom") {
if (h - e.y < borderSize + 1 /*rounding tolerance*/) {
pos = e.y;
document.addEventListener("mousemove", resize, false);
}
}
}, false);

document.addEventListener("mouseup", () => {
document.removeEventListener("mousemove", resize, false);
}, false);
}

!('nui_lib' in globalThis) && (globalThis.nui_lib = {});
globalThis.nui_lib.makeResizeable = makeResizeable;

export default makeResizeable;
12 changes: 12 additions & 0 deletions nui/src/nui/frontend/extensions/make_resizeable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <nui/frontend/extensions/make_resizeable.hpp>

namespace Nui
{
void makeResizeable(Nui::val const& element, ResizeableEdge edge)
{
Nui::val::global("nui_lib")["makeResizeable"](
element,
Nui::val{5},
Nui::val{edge == ResizeableEdge::Right ? std::string{"right"} : std::string{"bottom"}});
}
}
1 change: 1 addition & 0 deletions nui/src/nui/frontend/frontend_sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(NUI_FRONTEND_SOURCES_RELATIVE
components/dialog.cpp
dom/dom.cpp
event_system/event_context.cpp
extensions/make_resizeable.cpp
filesystem/file_dialog.cpp
filesystem/file.cpp
utility/fragment_listener.cpp
Expand Down

0 comments on commit f0ddf68

Please sign in to comment.