diff --git a/applets/cosmic-applet-graphics/src/main.rs b/applets/cosmic-applet-graphics/src/main.rs index dc59d6d9..e39ea6ab 100644 --- a/applets/cosmic-applet-graphics/src/main.rs +++ b/applets/cosmic-applet-graphics/src/main.rs @@ -5,6 +5,7 @@ extern crate relm4_macros; pub mod dbus; pub mod graphics; +pub mod mode_box; pub mod profile; use gtk4::{gio::ApplicationFlags, prelude::*, Orientation}; @@ -22,6 +23,18 @@ fn main() { application.run(); } +async fn get_current_graphics() -> zbus::Result { + let connection = zbus::Connection::system().await?; + let proxy = dbus::PowerDaemonProxy::new(&connection).await?; + graphics::get_current_graphics(&proxy).await +} + +async fn set_graphics(graphics_mode: graphics::Graphics) -> zbus::Result<()> { + let connection = zbus::Connection::system().await?; + let proxy = dbus::PowerDaemonProxy::new(&connection).await?; + graphics::set_graphics(&proxy, graphics_mode).await +} + fn build_ui(application: >k4::Application) { let window = gtk4::ApplicationWindow::builder() .application(application) @@ -29,7 +42,9 @@ fn build_ui(application: >k4::Application) { .default_width(400) .default_height(300) .build(); - + let current_graphics = RT + .block_on(get_current_graphics()) + .expect("failed to connect to system76-power"); view! { main_box = gtk4::Box { set_orientation: Orientation::Vertical, @@ -37,7 +52,13 @@ fn build_ui(application: >k4::Application) { set_margin_top: 20, set_margin_bottom: 20, set_margin_start: 24, - set_margin_end: 24 + set_margin_end: 24, + append: mode_label = >k4::Label { + set_text: "Graphics Mode" + }, + append: separator = >k4::Separator { + set_orientation: Orientation::Horizontal + } } } diff --git a/applets/cosmic-applet-graphics/src/mode_box.rs b/applets/cosmic-applet-graphics/src/mode_box.rs new file mode 100644 index 00000000..a8258388 --- /dev/null +++ b/applets/cosmic-applet-graphics/src/mode_box.rs @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later + +use gtk4::{ + glib::{self, Object}, + prelude::*, + subclass::prelude::*, + Align, CheckButton, Label, Orientation, +}; + +glib::wrapper! { + pub struct ModeSelection(ObjectSubclass) + @extends gtk4::Widget, + @implements gtk4::Accessible; +} + +impl ModeSelection { + pub fn new() -> Self { + Self::default() + } + + pub fn set_title(&self, title: &str) { + self.inner().label.set_text(title); + } + + pub fn set_description(&self, title: &str) { + let inner = self.inner(); + inner.description.set_text(title); + inner.description.show(); + } + + pub fn is_active(&self) -> bool { + self.inner().check.is_active() + } + + pub fn set_active(&self, setting: bool) { + self.inner().check.set_active(setting) + } + + pub fn set_group(&self, group: Option<&impl IsA>) { + self.inner().check.set_group(group) + } + + pub fn connect_toggled(&self, f: F) { + self.inner().check.connect_toggled(f); + } + + fn inner(&self) -> &ModeSelectionImp { + ModeSelectionImp::from_instance(self) + } +} + +impl Default for ModeSelection { + fn default() -> Self { + Object::new(&[]).expect("Failed to create `ModeSelection`.") + } +} + +#[derive(Debug, Default)] +pub struct ModeSelectionImp { + inner_box: gtk4::Box, + label_box: gtk4::Box, + label: Label, + description: Label, + check: CheckButton, +} + +#[glib::object_subclass] +impl ObjectSubclass for ModeSelectionImp { + const NAME: &'static str = "ModeSelection"; + type Type = ModeSelection; + type ParentType = gtk4::Widget; + type Interfaces = (); + type Instance = glib::subclass::basic::InstanceStruct; + type Class = glib::subclass::basic::ClassStruct; + + fn class_init(klass: &mut Self::Class) { + klass.set_layout_manager_type::(); + } +} + +impl ObjectImpl for ModeSelectionImp { + fn constructed(&self, obj: &Self::Type) { + self.parent_constructed(obj); + + self.check.set_halign(Align::End); + + self.label.set_halign(Align::Start); + self.label.add_css_class("title"); + + self.description.set_halign(Align::Start); + self.description.add_css_class("description"); + self.description.hide(); + + self.label_box.set_orientation(Orientation::Vertical); + self.label_box.append(&self.label); + self.label_box.append(&self.description); + + self.inner_box.set_orientation(Orientation::Horizontal); + self.inner_box.append(&self.label_box); + self.inner_box.append(&self.check); + } + + fn dispose(&self, _obj: &Self::Type) { + self.inner_box.remove(&self.label); + self.inner_box.remove(&self.check); + self.inner_box.unparent(); + } +} + +impl WidgetImpl for ModeSelectionImp {}