Skip to content

Commit

Permalink
eventlistener widget as first arg
Browse files Browse the repository at this point in the history
scroll's onChange, entry's onAccept, onChange, switch's onActivate
  • Loading branch information
Aylur committed Jul 25, 2023
1 parent 7a9999c commit af8eb56
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ export function timeout(ms: number, callback: () => void) {
});
}

export function runCmd(cmd: string | ((args: any[]) => void), ...args: any[]) {
export function runCmd(cmd: string | ((...args: any[]) => void), ...args: any[]) {
if (!cmd)
return;

if (typeof cmd === 'string')
return GLib.spawn_command_line_async(cmd);

if (typeof cmd === 'function')
return cmd(args);
return cmd(...args);
}

export function getConfig() {
Expand Down
31 changes: 15 additions & 16 deletions src/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Gdk from 'gi://Gdk?version=3.0';
import GLib from 'gi://GLib';
import GdkPixbuf from 'gi://GdkPixbuf';
import Widget from './widget.js';
import { typecheck, error, runCmd, restcheck, warning, getConfig, execAsync } from './utils.js';
import { typecheck, error, runCmd, restcheck, warning, getConfig } from './utils.js';

function _orientation(str) {
if (str === 'v')
Expand Down Expand Up @@ -59,12 +59,12 @@ export function EventBox({ type,

const box = new Gtk.EventBox();

box.connect('enter-notify-event', () => {
box.connect('enter-notify-event', box => {
box.set_state_flags(Gtk.StateFlags.PRELIGHT, false);
runCmd(onHover, box);
});

box.connect('leave-notify-event', () => {
box.connect('leave-notify-event', box => {
box.unset_state_flags(Gtk.StateFlags.PRELIGHT);
runCmd(onHoverLost, box);
});
Expand All @@ -85,7 +85,7 @@ export function EventBox({ type,

if (onScrollUp || onScrollDown) {
box.add_events(Gdk.EventMask.SCROLL_MASK);
box.connect('scroll-event', (_w, event) => {
box.connect('scroll-event', (box, event) => {
if (event.get_scroll_direction()[1] === Gdk.ScrollDirection.UP)
runCmd(onScrollUp, box);
else if (event.get_scroll_direction()[1] === Gdk.ScrollDirection.DOWN)
Expand Down Expand Up @@ -247,8 +247,7 @@ export function Slider({ type,
slider.connect('button-press-event', () => { slider._dragging = true; });
slider.connect('button-release-event', () => { slider._dragging = false; });

slider.connect('scroll-event', (_w, event) => {
const { adjustment } = slider;
slider.connect('scroll-event', ({ adjustment }, event) => {
const [, , y] = event.get_scroll_deltas();

slider._dragging = true;
Expand All @@ -265,7 +264,7 @@ export function Slider({ type,
return;

typeof onChange === 'function'
? onChange(value, slider)
? onChange(slider, value)
: runCmd(onChange.replace(/\{\}/g, value));
});
}
Expand Down Expand Up @@ -327,18 +326,18 @@ export function Entry({ type,
});

if (onAccept) {
entry.connect('activate', () => {
entry.connect('activate', ({ text }) => {
typeof onAccept === 'function'
? onAccept(entry.text, entry)
: runCmd(onAccept.replace(/\{\}/g, entry.text));
? onAccept(entry, text)
: runCmd(onAccept.replace(/\{\}/g, text));
});
}

if (onChange) {
entry.connect('notify::text', () => {
entry.connect('notify::text', ({ text }) => {
typeof onAccept === 'function'
? onChange(entry.text, entry)
: runCmd(onChange.replace(/\{\}/g, entry.text));
? onChange(entry, text)
: runCmd(onChange.replace(/\{\}/g, text));
});
}

Expand Down Expand Up @@ -446,10 +445,10 @@ export function Switch({ type,

const gtkswitch = new Gtk.Switch({ active });
if (onActivate) {
gtkswitch.connect('notify::active', () => {
gtkswitch.connect('notify::active', ({ active }) => {
typeof onActivate === 'function'
? onActivate(gtkswitch.active, gtkswitch)
: runCmd(onActivate.replace(/\{\}/g, gtkswitch.activate));
? onActivate(gtkswitch, active)
: runCmd(onActivate.replace(/\{\}/g, active));
});
}

Expand Down

0 comments on commit af8eb56

Please sign in to comment.