2023-11-16 14:37:09 -05:00
|
|
|
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-12-18 23:20:32 -05:00
|
|
|
import { EventBox } from 'resource:///com/github/Aylur/ags/widget.js';
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-12-18 23:20:32 -05:00
|
|
|
const { Gtk } = imports.gi;
|
2023-09-04 22:27:34 -04:00
|
|
|
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-11-16 14:37:09 -05:00
|
|
|
// TODO: wrap in another EventBox for disabled cursor
|
2023-12-18 18:00:30 -05:00
|
|
|
/**
|
2023-12-18 23:20:32 -05:00
|
|
|
* @typedef {import('types/widgets/eventbox').EventBoxProps} EventBox
|
2023-12-18 18:00:30 -05:00
|
|
|
*
|
2023-12-18 23:20:32 -05:00
|
|
|
* @param {EventBox & {
|
|
|
|
* on_primary_click_release?: function(EventBox):void
|
|
|
|
* }} o
|
2023-12-18 18:00:30 -05:00
|
|
|
*/
|
2023-10-17 13:47:02 -04:00
|
|
|
export default ({
|
2023-12-18 23:20:32 -05:00
|
|
|
attribute,
|
|
|
|
on_primary_click_release = () => {/**/},
|
2023-10-20 23:11:21 -04:00
|
|
|
...props
|
2023-10-17 13:47:02 -04:00
|
|
|
}) => {
|
2023-11-16 14:37:09 -05:00
|
|
|
// Make this variable to know if the function should
|
|
|
|
// be executed depending on where the click is released
|
|
|
|
const CanRun = Variable(true);
|
2023-12-18 23:20:32 -05:00
|
|
|
const Disabled = Variable(false);
|
2023-11-16 14:37:09 -05:00
|
|
|
|
2023-12-18 23:20:32 -05:00
|
|
|
const widget = EventBox({
|
2023-11-16 14:37:09 -05:00
|
|
|
...props,
|
2023-12-18 23:20:32 -05:00
|
|
|
sensitive: Disabled.bind().transform((v) => !v),
|
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
on_primary_click_release: (self) => {
|
2023-11-16 14:37:09 -05:00
|
|
|
// Every click, do a one shot connect to
|
|
|
|
// CanRun to wait for location of click
|
|
|
|
const id = CanRun.connect('changed', () => {
|
2023-11-21 01:29:46 -05:00
|
|
|
if (CanRun.value) {
|
2023-12-18 23:20:32 -05:00
|
|
|
on_primary_click_release(self);
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-16 14:37:09 -05:00
|
|
|
|
|
|
|
CanRun.disconnect(id);
|
|
|
|
});
|
|
|
|
},
|
2023-12-18 18:00:30 -05:00
|
|
|
});
|
2023-11-16 14:37:09 -05:00
|
|
|
|
2023-12-18 23:20:32 -05:00
|
|
|
const wrapper = EventBox({
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
|
|
|
attribute: {
|
|
|
|
...attribute,
|
|
|
|
|
|
|
|
disabled: Disabled,
|
|
|
|
|
|
|
|
get_child: () => widget.child,
|
|
|
|
|
|
|
|
set_child: (new_child) => {
|
|
|
|
widget.child = new_child;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
child: widget,
|
|
|
|
|
|
|
|
}).hook(Disabled, (self) => {
|
|
|
|
self.cursor = Disabled.value ?
|
|
|
|
'not-allowed' :
|
|
|
|
'pointer';
|
|
|
|
});
|
|
|
|
|
2023-11-16 14:37:09 -05:00
|
|
|
const gesture = Gtk.GestureLongPress.new(widget);
|
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
widget.hook(gesture, () => {
|
2023-11-16 14:37:09 -05:00
|
|
|
const pointer = gesture.get_point(null);
|
|
|
|
const x = pointer[1];
|
|
|
|
const y = pointer[2];
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
if ((!x || !y) || (x === 0 && y === 0)) {
|
2023-11-16 14:37:09 -05:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-16 14:37:09 -05:00
|
|
|
|
|
|
|
CanRun.value = !(
|
|
|
|
x > widget.get_allocated_width() ||
|
|
|
|
y > widget.get_allocated_height()
|
|
|
|
);
|
|
|
|
}, 'end');
|
|
|
|
|
2023-12-18 23:20:32 -05:00
|
|
|
return wrapper;
|
2023-10-20 23:11:21 -04:00
|
|
|
};
|