Skip to content

Commit

Permalink
library: Ported View switcher to rust (#594)
Browse files Browse the repository at this point in the history
* 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 56887ff.
  • Loading branch information
saadulkh authored Sep 17, 2023
1 parent e5ba6a3 commit 3b0f109
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
43 changes: 43 additions & 0 deletions src/Library/demos/View Switcher/code.rs
Original file line number Diff line number Diff line change
@@ -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(&notification_row);

if notifications_page.badge_number() == 0 {
notifications_page.set_needs_attention(false);
}
}
));

notification_row.add_suffix(&button);

notification_list.append(&notification_row);
}
}
7 changes: 3 additions & 4 deletions src/Library/demos/View Switcher/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions src/Library/demos/View Switcher/main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
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,
margin_top = 10,
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);
Expand Down

0 comments on commit 3b0f109

Please sign in to comment.