2022-05-26 11:59:09 -04:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2022-02-14 15:41:47 -05:00
|
|
|
|
2022-02-15 17:41:01 -05:00
|
|
|
#![allow(unused_parens, clippy::double_parens)] // needed for a quirk in the view! macro
|
|
|
|
|
|
2022-02-14 15:41:47 -05:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate relm4_macros;
|
|
|
|
|
|
|
|
|
|
pub mod dbus;
|
2022-02-15 10:59:57 -05:00
|
|
|
pub mod graphics;
|
2022-02-15 17:02:01 -05:00
|
|
|
pub mod mode_box;
|
2022-02-15 10:42:49 -05:00
|
|
|
|
2022-02-15 17:41:01 -05:00
|
|
|
use self::{dbus::PowerDaemonProxy, graphics::Graphics, mode_box::ModeSelection};
|
2022-02-15 18:24:24 -05:00
|
|
|
use gtk4::{
|
|
|
|
|
glib::{self, clone, MainContext, PRIORITY_DEFAULT},
|
|
|
|
|
prelude::*,
|
2022-07-05 16:21:23 -07:00
|
|
|
Align, Label, ListBox, ListBoxRow, Orientation, Overlay, Separator, Spinner,
|
2022-02-15 18:24:24 -05:00
|
|
|
};
|
2022-02-14 15:41:47 -05:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
use tokio::runtime::Runtime;
|
|
|
|
|
|
|
|
|
|
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));
|
|
|
|
|
|
2022-02-15 17:41:01 -05:00
|
|
|
async fn get_current_graphics() -> zbus::Result<Graphics> {
|
2022-02-15 17:02:01 -05:00
|
|
|
let connection = zbus::Connection::system().await?;
|
2022-02-15 17:41:01 -05:00
|
|
|
let proxy = PowerDaemonProxy::new(&connection).await?;
|
2022-02-15 17:02:01 -05:00
|
|
|
graphics::get_current_graphics(&proxy).await
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 17:41:01 -05:00
|
|
|
async fn set_graphics(graphics_mode: Graphics) -> zbus::Result<()> {
|
2022-02-15 17:02:01 -05:00
|
|
|
let connection = zbus::Connection::system().await?;
|
2022-02-15 17:41:01 -05:00
|
|
|
let proxy = PowerDaemonProxy::new(&connection).await?;
|
2022-02-15 17:02:01 -05:00
|
|
|
graphics::set_graphics(&proxy, graphics_mode).await
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 17:41:01 -05:00
|
|
|
fn row_clicked(_: &ListBox, row: &ListBoxRow) {
|
|
|
|
|
let child = row.child().expect("UNEXPECTED: row has no child");
|
|
|
|
|
let selector = child
|
|
|
|
|
.downcast::<ModeSelection>()
|
|
|
|
|
.expect("UNEXPECTED: child is not a mode selector");
|
|
|
|
|
selector.emit_activate();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 16:21:23 -07:00
|
|
|
fn main() {
|
2022-08-10 10:48:20 -04:00
|
|
|
let _ = gtk4::init();
|
|
|
|
|
adw::init();
|
|
|
|
|
|
2022-05-26 15:37:59 -04:00
|
|
|
let provider = gtk4::CssProvider::new();
|
2022-02-15 18:24:24 -05:00
|
|
|
provider.load_from_data(include_bytes!("style.css"));
|
2022-05-26 15:37:59 -04:00
|
|
|
gtk4::StyleContext::add_provider_for_display(
|
|
|
|
|
>k4::gdk::Display::default().expect("Could not connect to a display."),
|
2022-02-15 18:24:24 -05:00
|
|
|
&provider,
|
2022-05-26 15:37:59 -04:00
|
|
|
gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION,
|
2022-02-15 18:24:24 -05:00
|
|
|
);
|
|
|
|
|
|
2022-07-05 14:41:09 -07:00
|
|
|
let current_graphics = RT
|
2022-02-15 17:02:01 -05:00
|
|
|
.block_on(get_current_graphics())
|
|
|
|
|
.expect("failed to connect to system76-power");
|
2022-05-26 12:35:18 -04:00
|
|
|
|
2022-02-15 18:24:24 -05:00
|
|
|
let (tx, rx) = MainContext::channel::<bool>(PRIORITY_DEFAULT);
|
2022-05-26 12:35:18 -04:00
|
|
|
|
2022-02-14 15:41:47 -05:00
|
|
|
view! {
|
2022-07-05 16:21:23 -07:00
|
|
|
window = libcosmic_applet::AppletWindow {
|
|
|
|
|
set_title: Some("COSMIC Graphics Applet"),
|
2022-06-29 12:00:18 -07:00
|
|
|
#[wrap(Some)]
|
2022-07-05 16:21:23 -07:00
|
|
|
set_child = &libcosmic_applet::AppletButton {
|
|
|
|
|
set_button_icon_name: "input-gaming",
|
|
|
|
|
#[wrap(Some)]
|
|
|
|
|
set_popover_child: main_overlay = &Overlay {
|
|
|
|
|
add_overlay: loading_box = >k4::Box {
|
|
|
|
|
append: loading_explain_box = >k4::Box {
|
|
|
|
|
set_orientation: Orientation::Vertical,
|
|
|
|
|
set_halign: Align::Center,
|
|
|
|
|
set_valign: Align::Center,
|
|
|
|
|
append: loading_spinner = &Spinner {
|
|
|
|
|
set_halign: Align::Center,
|
|
|
|
|
},
|
|
|
|
|
append: loading_explain = &Label {
|
|
|
|
|
set_label: "Please wait while your graphics mode is set...",
|
|
|
|
|
set_halign: Align::Center,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
set_halign: Align::Center,
|
|
|
|
|
set_valign: Align::Center,
|
|
|
|
|
set_hexpand: true,
|
|
|
|
|
set_vexpand: true,
|
|
|
|
|
set_visible: false,
|
|
|
|
|
add_css_class: "loading-overlay",
|
2022-02-15 18:24:24 -05:00
|
|
|
},
|
2022-07-05 16:21:23 -07:00
|
|
|
#[wrap(Some)]
|
|
|
|
|
set_child: main_box = >k4::Box {
|
|
|
|
|
set_orientation: Orientation::Vertical,
|
|
|
|
|
set_spacing: 10,
|
|
|
|
|
set_margin_top: 20,
|
|
|
|
|
set_margin_bottom: 20,
|
|
|
|
|
set_margin_start: 24,
|
|
|
|
|
set_margin_end: 24,
|
|
|
|
|
append: mode_label = &Label {
|
|
|
|
|
set_text: "Graphics Mode"
|
|
|
|
|
},
|
|
|
|
|
append: separator = &Separator {
|
|
|
|
|
set_orientation: Orientation::Horizontal
|
|
|
|
|
},
|
|
|
|
|
append: graphics_modes_list = &ListBox {
|
|
|
|
|
connect_row_activated: row_clicked,
|
|
|
|
|
append: integrated_selector = &ModeSelection {
|
|
|
|
|
set_title: "Integrated Graphics",
|
|
|
|
|
set_description: "Disables external displays. Requires Restart.",
|
|
|
|
|
set_active: (current_graphics == Graphics::Integrated),
|
|
|
|
|
connect_toggled: clone!(@strong tx => move |_| {
|
|
|
|
|
tx.send(true).expect("failed to send to main context");
|
|
|
|
|
let tx = tx.clone();
|
|
|
|
|
RT.spawn(async move {
|
|
|
|
|
set_graphics(Graphics::Integrated).await.expect("failed to set graphics mode");
|
|
|
|
|
tx.send(false).expect("failed to send to main context");
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
append: nvidia_selector = &ModeSelection {
|
|
|
|
|
set_title: "NVIDIA Graphics",
|
|
|
|
|
set_group: Some(&integrated_selector),
|
|
|
|
|
set_active: (current_graphics == Graphics::Nvidia),
|
|
|
|
|
connect_toggled: clone!(@strong tx => move |_| {
|
|
|
|
|
tx.send(true).expect("failed to send to main context");
|
|
|
|
|
let tx = tx.clone();
|
|
|
|
|
RT.spawn(async move {
|
|
|
|
|
set_graphics(Graphics::Nvidia).await.expect("failed to set graphics mode");
|
|
|
|
|
tx.send(false).expect("failed to send to main context");
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
append: hybrid_selector = &ModeSelection {
|
|
|
|
|
set_title: "Hybrid Graphics",
|
|
|
|
|
set_description: "Requires Restart.",
|
|
|
|
|
set_group: Some(&integrated_selector),
|
|
|
|
|
set_active: (current_graphics == Graphics::Hybrid),
|
|
|
|
|
connect_toggled: clone!(@strong tx => move |_| {
|
|
|
|
|
tx.send(true).expect("failed to send to main context");
|
|
|
|
|
let tx = tx.clone();
|
|
|
|
|
RT.spawn(async move {
|
|
|
|
|
set_graphics(Graphics::Hybrid).await.expect("failed to set graphics mode");
|
|
|
|
|
tx.send(false).expect("failed to send to main context");
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
append: compute_selector = &ModeSelection {
|
|
|
|
|
set_title: "Compute Graphics",
|
|
|
|
|
set_description: "Disables external displays. Requires Restart.",
|
|
|
|
|
set_group: Some(&integrated_selector),
|
|
|
|
|
set_active: (current_graphics == Graphics::Compute),
|
|
|
|
|
connect_toggled: clone!(@strong tx => move |_| {
|
|
|
|
|
tx.send(true).expect("failed to send to main context");
|
|
|
|
|
let tx = tx.clone();
|
|
|
|
|
RT.spawn(async move {
|
|
|
|
|
set_graphics(Graphics::Compute).await.expect("failed to set graphics mode");
|
|
|
|
|
tx.send(false).expect("failed to send to main context");
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-15 18:24:24 -05:00
|
|
|
}
|
2022-02-15 17:02:01 -05:00
|
|
|
}
|
2022-02-14 15:41:47 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-05 16:21:23 -07:00
|
|
|
|
2022-02-15 18:24:24 -05:00
|
|
|
rx.attach(
|
|
|
|
|
None,
|
|
|
|
|
clone!(@weak loading_box, @weak loading_spinner => @default-return Continue(true), move |val| {
|
|
|
|
|
loading_box.set_visible(val);
|
|
|
|
|
loading_spinner.set_spinning(val);
|
|
|
|
|
Continue(true)
|
|
|
|
|
}),
|
|
|
|
|
);
|
2022-02-14 15:41:47 -05:00
|
|
|
|
|
|
|
|
window.show();
|
2022-07-05 16:21:23 -07:00
|
|
|
|
|
|
|
|
let main_loop = glib::MainLoop::new(None, false);
|
|
|
|
|
main_loop.run();
|
2022-02-14 15:41:47 -05:00
|
|
|
}
|