|
| 1 | +#! /usr/bin/env -S vala workbench.vala --pkg libadwaita-1 |
| 2 | +private bool auto_scrolling = false; |
| 3 | + |
| 4 | +public void main () { |
| 5 | + var scrolled_window = (Gtk.ScrolledWindow) workbench.builder.get_object ("scrolled_window"); |
| 6 | + var container = (Gtk.Box) workbench.builder.get_object ("container"); |
| 7 | + var toggle_orientation = (Gtk.ToggleButton) workbench.builder.get_object ("toggle_orientation"); |
| 8 | + var button_start = (Gtk.Button) workbench.builder.get_object ("button_start"); |
| 9 | + var button_end = (Gtk.Button) workbench.builder.get_object ("button_end"); |
| 10 | + |
| 11 | + button_start.sensitive = false; |
| 12 | + |
| 13 | + HashTable<Gtk.Orientation, Gtk.Scrollbar> scrollbars = new HashTable<Gtk.Orientation, Gtk.Scrollbar> (direct_hash, direct_equal); |
| 14 | + scrollbars[Gtk.Orientation.HORIZONTAL] = (Gtk.Scrollbar) scrolled_window.get_hscrollbar (); |
| 15 | + scrollbars[Gtk.Orientation.VERTICAL] = (Gtk.Scrollbar) scrolled_window.get_vscrollbar (); |
| 16 | + |
| 17 | + toggle_orientation.toggled.connect (() => { |
| 18 | + if (toggle_orientation.active) { |
| 19 | + container.orientation = Gtk.Orientation.HORIZONTAL; |
| 20 | + } else { |
| 21 | + container.orientation = Gtk.Orientation.VERTICAL; |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + int num_items = 20; |
| 26 | + for (int i = 0; i < num_items; i++) { |
| 27 | + populate_container (container, @"Item $(i + 1)"); |
| 28 | + } |
| 29 | + |
| 30 | + foreach (Gtk.Orientation orientation in scrollbars.get_keys ()) { |
| 31 | + Gtk.Scrollbar scrollbar = scrollbars[orientation]; |
| 32 | + Gtk.Adjustment adj = scrollbar.adjustment; |
| 33 | + adj.value_changed.connect (() => { |
| 34 | + if (adj.value == adj.lower) { |
| 35 | + button_end.sensitive = true; |
| 36 | + button_start.sensitive = false; |
| 37 | + } else if (adj.value == adj.upper - adj.page_size) { |
| 38 | + button_end.sensitive = false; |
| 39 | + button_start.sensitive = true; |
| 40 | + } else { |
| 41 | + // Disable buttons if scrollbar is auto-scrolling |
| 42 | + button_end.sensitive = !auto_scrolling; |
| 43 | + button_start.sensitive = !auto_scrolling; |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + scrolled_window.edge_reached.connect (() => { |
| 49 | + message (@"Edge Reached"); |
| 50 | + }); |
| 51 | + |
| 52 | + button_start.clicked.connect (() => { |
| 53 | + auto_scrolling = true; |
| 54 | + Gtk.Scrollbar scrollbar = scrollbars[container.orientation]; |
| 55 | + Adw.Animation anim = create_scrollbar_anim (scrollbar, false); |
| 56 | + anim.play (); |
| 57 | + }); |
| 58 | + |
| 59 | + button_end.clicked.connect (() => { |
| 60 | + auto_scrolling = true; |
| 61 | + Gtk.Scrollbar scrollbar = scrollbars[container.orientation]; |
| 62 | + Adw.Animation anim = create_scrollbar_anim (scrollbar, true); |
| 63 | + anim.play (); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +void populate_container (Gtk.Box container, string label) { |
| 68 | + Gtk.Widget item = new Adw.Bin () { |
| 69 | + margin_top = 6, |
| 70 | + margin_bottom = 6, |
| 71 | + margin_start = 6, |
| 72 | + margin_end = 6, |
| 73 | + child = new Gtk.Label (label) { |
| 74 | + width_request = 100, |
| 75 | + height_request = 100 |
| 76 | + }, |
| 77 | + css_classes = { "card" } |
| 78 | + }; |
| 79 | + |
| 80 | + container.append (item); |
| 81 | +} |
| 82 | + |
| 83 | +Adw.TimedAnimation create_scrollbar_anim (Gtk.Scrollbar scrollbar, bool direction) { |
| 84 | + // direction = 0 -> Animates to Start |
| 85 | + // direction = 1 -> Animates to End |
| 86 | + Gtk.Adjustment adjustment = scrollbar.adjustment; |
| 87 | + double value_to = 0; |
| 88 | + if (direction) { |
| 89 | + value_to = adjustment.upper - adjustment.page_size; |
| 90 | + } else { |
| 91 | + value_to = 0; |
| 92 | + } |
| 93 | + |
| 94 | + var target = new Adw.PropertyAnimationTarget (adjustment, "value"); |
| 95 | + var animation = new Adw.TimedAnimation ( |
| 96 | + scrollbar, // widget |
| 97 | + adjustment.value, // value_from |
| 98 | + value_to, // value_to |
| 99 | + 1000, // duration |
| 100 | + target // target |
| 101 | + ) { |
| 102 | + easing = LINEAR |
| 103 | + }; |
| 104 | + |
| 105 | + animation.done.connect (() => { |
| 106 | + auto_scrolling = false; |
| 107 | + }); |
| 108 | + |
| 109 | + return animation; |
| 110 | +} |
0 commit comments