From 9ed29eab94ecd8e3e24dde8ce4fc20e30b1ec424 Mon Sep 17 00:00:00 2001 From: Lucy Date: Tue, 1 Feb 2022 16:46:25 -0500 Subject: [PATCH] Basic UI work --- Cargo.lock | 5 +-- applets/cosmic-applet-network/Cargo.toml | 1 + applets/cosmic-applet-network/src/main.rs | 34 ++++++++++++++++--- applets/cosmic-applet-network/src/task.rs | 14 ++++++++ applets/cosmic-applet-network/src/ui.rs | 3 ++ .../cosmic-applet-network/src/ui/toggles.rs | 27 +++++++++++++++ 6 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 applets/cosmic-applet-network/src/task.rs create mode 100644 applets/cosmic-applet-network/src/ui.rs create mode 100644 applets/cosmic-applet-network/src/ui/toggles.rs diff --git a/Cargo.lock b/Cargo.lock index cdf08bc6..1a7d46bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,9 +183,9 @@ checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" [[package]] name = "cfg-expr" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edae0b9625d1fce32f7d64b71784d9b1bf8469ec1a9c417e44aaf16a9cbd7571" +checksum = "3431df59f28accaf4cb4eed4a9acc66bea3f3c3753aa6cdc2f024174ef232af7" dependencies = [ "smallvec", ] @@ -224,6 +224,7 @@ version = "0.1.0" dependencies = [ "cosmic-dbus-networkmanager", "gtk4", + "once_cell", "relm4-macros", "tokio", "zbus", diff --git a/applets/cosmic-applet-network/Cargo.toml b/applets/cosmic-applet-network/Cargo.toml index fc0807db..ab0bdef7 100644 --- a/applets/cosmic-applet-network/Cargo.toml +++ b/applets/cosmic-applet-network/Cargo.toml @@ -7,6 +7,7 @@ license = "LGPL-3.0-or-later" [dependencies] cosmic-dbus-networkmanager = { git = "https://github.com/pop-os/dbus-settings-bindings" } gtk4 = "0.4.6" +once_cell = "1.9.0" relm4-macros = "0.4.1" tokio = { version = "1.15.0", features = ["full"] } zbus = "2.0.1" diff --git a/applets/cosmic-applet-network/src/main.rs b/applets/cosmic-applet-network/src/main.rs index 46840ffd..bb608f9e 100644 --- a/applets/cosmic-applet-network/src/main.rs +++ b/applets/cosmic-applet-network/src/main.rs @@ -1,7 +1,16 @@ -use gtk4::{ - gdk::Display, gio::ApplicationFlags, prelude::*, CssProvider, StyleContext, - STYLE_PROVIDER_PRIORITY_APPLICATION, -}; +// SPDX-License-Identifier: LGPL-3.0-or-later + +#[macro_use] +extern crate relm4_macros; + +pub mod task; +pub mod ui; + +use gtk4::{gio::ApplicationFlags, prelude::*, Orientation}; +use once_cell::sync::Lazy; +use tokio::runtime::Runtime; + +static RT: Lazy = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime")); fn main() { let application = gtk4::Application::new( @@ -17,6 +26,21 @@ fn build_ui(application: >k4::Application) { .application(application) .title("COSMIC Network Applet") .default_width(400) - .default_height(600) + .default_height(300) .build(); + + view! { + main_box = gtk4::Box { + set_orientation: Orientation::Vertical, + set_spacing: 20, + set_margin_top: 20, + set_margin_bottom: 20, + set_margin_start: 24, + set_margin_end: 24 + } + } + ui::toggles::add_toggles(&main_box); + window.set_child(Some(&main_box)); + + window.show(); } diff --git a/applets/cosmic-applet-network/src/task.rs b/applets/cosmic-applet-network/src/task.rs new file mode 100644 index 00000000..9d75bfe4 --- /dev/null +++ b/applets/cosmic-applet-network/src/task.rs @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +use std::future::Future; + +pub fn spawn(future: F) -> tokio::task::JoinHandle +where + F: Future + Send + 'static, + O: Send + 'static, +{ + crate::RT.spawn(future) +} + +pub fn spawn_local + 'static>(future: F) { + gtk4::glib::MainContext::default().spawn_local(future); +} diff --git a/applets/cosmic-applet-network/src/ui.rs b/applets/cosmic-applet-network/src/ui.rs new file mode 100644 index 00000000..31363901 --- /dev/null +++ b/applets/cosmic-applet-network/src/ui.rs @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later + +pub mod toggles; diff --git a/applets/cosmic-applet-network/src/ui/toggles.rs b/applets/cosmic-applet-network/src/ui/toggles.rs new file mode 100644 index 00000000..2f84118d --- /dev/null +++ b/applets/cosmic-applet-network/src/ui/toggles.rs @@ -0,0 +1,27 @@ +use gtk4::{prelude::*, Align, Label, Orientation, Separator, Switch}; + +pub fn add_toggles(target: >k4::Box) { + view! { + airplane_mode_box = gtk4::Box { + append: airplane_mode_label = &Label { + set_markup: "Airplane Mode", + set_halign: Align::Start + }, + append: airplane_mode_switch = &Switch {} + } + } + view! { + wifi_box = gtk4::Box { + append: wifi_label = &Label { + set_markup: "WiFi", + set_halign: Align::Start + }, + append: wifi_switch = &Switch {} + } + } + + target.append(&airplane_mode_box); + target.append(&Separator::new(Orientation::Horizontal)); + target.append(&wifi_box); + target.append(&Separator::new(Orientation::Horizontal)); +}