Mode selection widget, base async zbus code
This commit is contained in:
parent
f4b895d61d
commit
29f0bbf0f6
2 changed files with 133 additions and 2 deletions
|
|
@ -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<graphics::Graphics> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
110
applets/cosmic-applet-graphics/src/mode_box.rs
Normal file
110
applets/cosmic-applet-graphics/src/mode_box.rs
Normal file
|
|
@ -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<ModeSelectionImp>)
|
||||
@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<CheckButton>>) {
|
||||
self.inner().check.set_group(group)
|
||||
}
|
||||
|
||||
pub fn connect_toggled<F: Fn(&CheckButton) + 'static>(&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<Self>;
|
||||
type Class = glib::subclass::basic::ClassStruct<Self>;
|
||||
|
||||
fn class_init(klass: &mut Self::Class) {
|
||||
klass.set_layout_manager_type::<gtk4::BinLayout>();
|
||||
}
|
||||
}
|
||||
|
||||
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 {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue