feat(ags cursorbox): when release click outside box, don't run func

This commit is contained in:
matt1432 2023-11-16 14:37:09 -05:00
parent 11f5eecbc2
commit b255853935
2 changed files with 58 additions and 32 deletions

View file

@ -3,6 +3,7 @@ import { Box, Icon, Label } from 'resource:///com/github/Aylur/ags/widget.js';
const DEFAULT_KB = 'at-translated-set-2-keyboard'; const DEFAULT_KB = 'at-translated-set-2-keyboard';
export default () => Box({ export default () => Box({
className: 'toggle-off', className: 'toggle-off',
css: 'padding: 0 10px;', css: 'padding: 0 10px;',

View file

@ -1,19 +1,36 @@
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
import { Button, EventBox } from 'resource:///com/github/Aylur/ags/widget.js'; import { Button, EventBox } from 'resource:///com/github/Aylur/ags/widget.js';
import Gtk from 'gi://Gtk';
import Gdk from 'gi://Gdk'; import Gdk from 'gi://Gdk';
const display = Gdk.Display.get_default(); const display = Gdk.Display.get_default();
// TODO: wrap in another EventBox for disabled cursor
export default ({ export default ({
isButton = false, isButton = false,
reset = true, reset = true,
onHover = () => {}, onHover = () => {},
onHoverLost = () => {}, onHoverLost = () => {},
onPrimaryClickRelease = () => {},
...props ...props
}) => { }) => {
if (!isButton) { // Make this variable to know if the function should
return EventBox({ // be executed depending on where the click is released
const CanRun = Variable(true);
const properties = {
...props, ...props,
onPrimaryClickRelease: self => {
// Every click, do a one shot connect to
// CanRun to wait for location of click
const id = CanRun.connect('changed', () => {
if (CanRun.value)
onPrimaryClickRelease(self);
CanRun.disconnect(id);
});
},
onHover: self => { onHover: self => {
if (!self.child.sensitive || !self.sensitive) if (!self.child.sensitive || !self.sensitive)
self.window.set_cursor(Gdk.Cursor.new_from_name(display, 'not-allowed')); self.window.set_cursor(Gdk.Cursor.new_from_name(display, 'not-allowed'));
@ -29,21 +46,29 @@ export default ({
onHoverLost(self); onHoverLost(self);
}, },
}); };
}
else { let widget;
return Button({ if (!isButton)
...props, widget = EventBox(properties);
onHover: self => {
if (!self.child.sensitive || !self.sensitive)
self.window.set_cursor(Gdk.Cursor.new_from_name(display, 'not-allowed'));
else else
self.window.set_cursor(Gdk.Cursor.new_from_name(display, 'pointer')); widget = Button(properties);
},
onHoverLost: self => { const gesture = Gtk.GestureLongPress.new(widget);
if (reset)
self.window.set_cursor(null); widget.connectTo(gesture, () => {
}, const pointer = gesture.get_point(null);
}); const x = pointer[1];
} const y = pointer[2];
if ((!x || !y) || x === 0 && y === 0)
return;
CanRun.value = !(
x > widget.get_allocated_width() ||
y > widget.get_allocated_height()
);
}, 'end');
return widget;
}; };