summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/system.ts5
-rw-r--r--src/utils/widgets.ts46
2 files changed, 30 insertions, 21 deletions
diff --git a/src/utils/system.ts b/src/utils/system.ts
index 2e9fa4a..8ccb5d1 100644
--- a/src/utils/system.ts
+++ b/src/utils/system.ts
@@ -76,6 +76,7 @@ export const bindCurrentTime = (
return bind(time);
};
+const monitors = new Set();
export const monitorDirectory = (
path: string,
callback: (
@@ -102,5 +103,9 @@ export const monitorDirectory = (
}
}
+ // Keep ref to monitor so it doesn't get GCed
+ monitors.add(monitor);
+ monitor.connect("notify::cancelled", () => monitor.cancelled && monitors.delete(monitor));
+
return monitor;
};
diff --git a/src/utils/widgets.ts b/src/utils/widgets.ts
index 7b1eb5c..e0e512d 100644
--- a/src/utils/widgets.ts
+++ b/src/utils/widgets.ts
@@ -3,7 +3,11 @@ import { Astal, astalify, Gtk, Widget, type ConstructProps } from "astal/gtk3";
import AstalHyprland from "gi://AstalHyprland";
import type { AstalWidget } from "./types";
-export const setupCustomTooltip = (self: AstalWidget, text: string | Binding<string>) => {
+export const setupCustomTooltip = (
+ self: AstalWidget,
+ text: string | Binding<string>,
+ labelProps: Widget.LabelProps = {}
+) => {
if (!text) return null;
self.set_has_tooltip(true);
@@ -15,39 +19,39 @@ export const setupCustomTooltip = (self: AstalWidget, text: string | Binding<str
keymode: Astal.Keymode.NONE,
exclusivity: Astal.Exclusivity.IGNORE,
anchor: Astal.WindowAnchor.TOP | Astal.WindowAnchor.LEFT,
- child: new Widget.Label({ className: "tooltip", label: text }),
+ child: new Widget.Label({ ...labelProps, className: "tooltip", label: text }),
});
self.set_tooltip_window(window);
- let lastX = 0;
- window.connect("size-allocate", () => {
- const mWidth = AstalHyprland.get_default().get_focused_monitor().get_width();
- const pWidth = window.get_preferred_width()[1];
+ if (text instanceof Binding) self.hook(text, (_, v) => !v && window.hide());
+
+ const positionWindow = ({ x, y }: { x: number; y: number }) => {
+ const { width: mWidth, height: mHeight } = AstalHyprland.get_default().get_focused_monitor();
+ const { width: pWidth, height: pHeight } = window.get_preferred_size()[1]!;
+ const cursorSize = Gtk.Settings.get_default()?.gtkCursorThemeSize ?? 0;
- let marginLeft = lastX - pWidth / 2;
+ let marginLeft = x - pWidth / 2;
if (marginLeft < 0) marginLeft = 0;
else if (marginLeft + pWidth > mWidth) marginLeft = mWidth - pWidth;
+ let marginTop = y + cursorSize;
+ if (marginTop < 0) marginTop = 0;
+ else if (marginTop + pHeight > mHeight) marginTop = y - pHeight;
+
window.marginLeft = marginLeft;
- });
- if (text instanceof Binding) self.hook(text, (_, v) => !v && window.hide());
+ window.marginTop = marginTop;
+ };
+
+ let lastPos = { x: 0, y: 0 };
+ window.connect("size-allocate", () => positionWindow(lastPos));
self.connect("query-tooltip", () => {
if (text instanceof Binding && !text.get()) return false;
if (window.visible) return true;
- const mWidth = AstalHyprland.get_default().get_focused_monitor().get_width();
- const pWidth = window.get_preferred_width()[1];
- const { x, y } = AstalHyprland.get_default().get_cursor_position();
- const cursorSize = Gtk.Settings.get_default()?.gtkCursorThemeSize ?? 0;
-
- let marginLeft = x - pWidth / 2;
- if (marginLeft < 0) marginLeft = 0;
- else if (marginLeft + pWidth > mWidth) marginLeft = mWidth - pWidth;
-
- window.marginLeft = marginLeft;
- window.marginTop = y + cursorSize;
- lastX = x;
+ const cPos = AstalHyprland.get_default().get_cursor_position();
+ positionWindow(cPos);
+ lastPos = cPos;
return true;
});