From 77ac328e445ae1d6996eb005243fae9ee9254ab4 Mon Sep 17 00:00:00 2001 From: Aylur Date: Fri, 28 Jul 2023 13:06:26 +0200 Subject: [PATCH] stack widget --- src/widget.ts | 1 + src/widgets.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/widget.ts b/src/widget.ts index 558bb0d..a2bc492 100644 --- a/src/widget.ts +++ b/src/widget.ts @@ -38,6 +38,7 @@ const widgets: { [key: string]: (props: any) => Gtk.Widget } = { 'revealer': Basic.Revealer, 'scrollable': Basic.Scrollable, 'slider': Basic.Slider, + 'stack': Basic.Stack, 'switch': Basic.Switch, }; diff --git a/src/widgets.js b/src/widgets.js index 2e443b6..921e205 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -304,6 +304,51 @@ export function Dynamic({ type, items = [], ...rest } = {}) { return box; } +export function Stack({ type, + items = [], + hhomogeneous = true, + vhomogeneous = true, + interpolateSize = false, + transition = 'none', + transitionDuration = 200, + ...rest +}) { + typecheck('hhomogeneous', hhomogeneous, 'boolean', type); + typecheck('vhomogeneous', vhomogeneous, 'boolean', type); + typecheck('interpolateSize', interpolateSize, 'boolean', type); + typecheck('transition', transition, 'string', type); + typecheck('transitionDuration', transitionDuration, 'number', type); + typecheck('items', items, 'array', type); + restcheck(rest, type); + + const stack = new Gtk.Stack({ + hhomogeneous, + vhomogeneous, + interpolateSize, + transitionDuration, + }); + + try { + stack.transitionType = Gtk.StackTransitionType[transition.toUpperCase()]; + } catch (error) { + error('wrong interpolate value'); + } + + items.forEach(([name, widget]) => { + if (widget) + stack.add_named(Widget(widget), name); + }); + + stack.showChild = name => { + const n = typeof name === 'function' ? name() : name; + stack.visible = true; + stack.get_child_by_name(n) + ? stack.set_visible_child_name(n) + : stack.visible = false; + }; + return stack; +} + export function Entry({ type, text = '', placeholder = '',