From 3b0f10932445f011a7064a59d3c97cd27a4a940c Mon Sep 17 00:00:00 2001 From: Saad Khan <38633812+saadulkh@users.noreply.github.com> Date: Mon, 18 Sep 2023 00:24:01 +0500 Subject: [PATCH] library: Ported View switcher to rust (#594) * library: Ported ViewSwitcher to rust * library: Moved `title` * Updated refrences to `strong` * Updated `notification_count` to an integer value * Removed the unused reference * Works with weak refrences now as well * Sync JS * Changed indent to `2` to match other varients * Sync vala * Test says so * Revert "Changed indent to `2` to match other varients" This reverts commit 56887ff99c6977edb4541d3840a6cb790801888d. --- src/Library/demos/View Switcher/code.rs | 43 +++++++++++++++++++++++ src/Library/demos/View Switcher/main.js | 7 ++-- src/Library/demos/View Switcher/main.vala | 16 ++++----- 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 src/Library/demos/View Switcher/code.rs diff --git a/src/Library/demos/View Switcher/code.rs b/src/Library/demos/View Switcher/code.rs new file mode 100644 index 000000000..7950b039c --- /dev/null +++ b/src/Library/demos/View Switcher/code.rs @@ -0,0 +1,43 @@ +use crate::workbench; +use adw::prelude::*; + +use glib::clone; +use gtk::glib; + +pub fn main() { + let notifications_page: adw::ViewStackPage = workbench::builder().object("page3").unwrap(); + let notification_list: gtk::ListBox = workbench::builder().object("notification_list").unwrap(); + + let notification_count = 5; + notifications_page.set_badge_number(notification_count); + + for _ in 0..notification_count { + let notification_row = adw::ActionRow::builder() + .title("Notification") + .selectable(false) + .build(); + + let button = gtk::Button::builder() + .halign(gtk::Align::Center) + .valign(gtk::Align::Center) + .margin_top(10) + .margin_bottom(10) + .icon_name("check-plain-symbolic") + .build(); + + button.connect_clicked(clone!( + @weak notifications_page, @weak notification_list, @weak notification_row => move |_| { + notifications_page.set_badge_number(notifications_page.badge_number() - 1); + notification_list.remove(¬ification_row); + + if notifications_page.badge_number() == 0 { + notifications_page.set_needs_attention(false); + } + } + )); + + notification_row.add_suffix(&button); + + notification_list.append(¬ification_row); + } +} diff --git a/src/Library/demos/View Switcher/main.js b/src/Library/demos/View Switcher/main.js index b1981216c..43f1edf87 100644 --- a/src/Library/demos/View Switcher/main.js +++ b/src/Library/demos/View Switcher/main.js @@ -4,16 +4,15 @@ import Gtk from "gi://Gtk"; const notifications_page = workbench.builder.get_object("page3"); const notification_list = workbench.builder.get_object("notification_list"); -let notification_count = 5; +const notification_count = 5; notifications_page.badge_number = notification_count; for (let i = 0; i < notification_count; i++) { const notification_row = new Adw.ActionRow({ + title: "Notification", selectable: false, }); - notification_row.title = "Notification"; - const button = new Gtk.Button({ halign: "center", valign: "center", @@ -23,7 +22,7 @@ for (let i = 0; i < notification_count; i++) { }); button.connect("clicked", () => { - notifications_page.badge_number = --notification_count; + notifications_page.badge_number -= 1; notification_list.remove(notification_row); if (notifications_page.badge_number === 0) { diff --git a/src/Library/demos/View Switcher/main.vala b/src/Library/demos/View Switcher/main.vala index 89e32b162..ddec60ecc 100644 --- a/src/Library/demos/View Switcher/main.vala +++ b/src/Library/demos/View Switcher/main.vala @@ -3,17 +3,16 @@ public void main() { var notifications_page = workbench.builder.get_object("page3") as Adw.ViewStackPage; var notification_list = workbench.builder.get_object("notification_list") as Gtk.ListBox; - + int notification_count = 5; - notifications_page.badge_number = notification_count; - + for (int i = 0; i < notification_count; i++){ var notification_row = new Adw.ActionRow(){ title = "Notification", selectable = false, }; - + var button = new Gtk.Button(){ halign = Gtk.Align.CENTER, valign = Gtk.Align.CENTER, @@ -21,15 +20,16 @@ public void main() { margin_bottom = 10, icon_name = "check-plain-symbolic" }; - + button.clicked.connect( () => { - notifications_page.badge_number = --notification_count; + notifications_page.badge_number -= 1; notification_list.remove(notification_row); - if (notifications_page.badge_number == 0) + if (notifications_page.badge_number == 0) { notifications_page.needs_attention = false; + } }); - + notification_row.add_suffix(button); notification_list.append(notification_row);