Use libcomsic-applet in status-area, network, graphics

This commit is contained in:
Ian Douglas Scott 2022-07-05 16:21:23 -07:00
parent aac43de65d
commit 765e3af815
13 changed files with 154 additions and 330 deletions

View file

@ -7,90 +7,43 @@ pub mod task;
pub mod ui;
pub mod widgets;
use cosmic_panel_config::config::CosmicPanelConfig;
use gtk4::{gio::ApplicationFlags, glib, prelude::*, Orientation, Separator};
use gtk4::{glib, prelude::*, Orientation, Separator};
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));
fn main() {
let application = gtk4::Application::new(
Some("com.system76.cosmic.applets.network"),
ApplicationFlags::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn build_ui(application: &gtk4::Application) {
let provider = gtk4::CssProvider::new();
provider.load_from_data(include_bytes!("style.css"));
gtk4::StyleContext::add_provider_for_display(
&gtk4::gdk::Display::default().expect("Could not connect to a display."),
&provider,
gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
let window = gtk4::ApplicationWindow::builder()
.application(application)
.title("COSMIC Network Applet")
.decorated(false)
.resizable(false)
.width_request(1)
.height_request(1)
.css_classes(vec!["root_window".to_string()])
.build();
gtk4::init().unwrap();
view! {
main_box = gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 10,
set_margin_top: 20,
set_margin_bottom: 20,
set_margin_start: 24,
set_margin_end: 24
window = libcosmic_applet::AppletWindow {
set_title: Some("COSMIC Network Applet"),
#[wrap(Some)]
set_child: button = &libcosmic_applet::AppletButton {
set_button_icon_name: "preferences-system-network",
#[wrap(Some)]
set_popover_child: main_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 10,
set_margin_top: 20,
set_margin_bottom: 20,
set_margin_start: 24,
set_margin_end: 24
}
}
}
}
let config = CosmicPanelConfig::load_from_env().unwrap_or_default();
let popover = gtk4::builders::PopoverBuilder::new()
.autohide(true)
.has_arrow(false)
.build();
let button = gtk4::Button::new();
button.add_css_class("panel_icon");
button.connect_clicked(glib::clone!(@weak popover => move |_| {
popover.show();
}));
// TODO cleanup
let image = gtk4::Image::from_icon_name("preferences-system-network");
image.add_css_class("panel_icon");
image.set_pixel_size(config.get_applet_icon_size().try_into().unwrap());
button.set_child(Some(&image));
view! {
icon_box = gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 0,
add_css_class: "icon_box",
}
}
popover.set_child(Some(&main_box));
icon_box.append(&button);
icon_box.append(&popover);
ui::current_networks::add_current_networks(&main_box, &image);
ui::current_networks::add_current_networks(&main_box, &button);
main_box.append(&Separator::new(Orientation::Horizontal));
ui::toggles::add_toggles(&main_box);
let available_wifi_separator = Separator::new(Orientation::Horizontal);
main_box.append(&available_wifi_separator);
available_wifi_separator.hide();
ui::available_wifi::add_available_wifi(&main_box, available_wifi_separator);
window.set_child(Some(&icon_box));
window.show();
let main_loop = glib::MainLoop::new(None, false);
main_loop.run();
}

View file

@ -1,28 +0,0 @@
image.panel_icon {
padding-left: 0px;
padding-right: 0px;
padding-top: 0px;
padding-bottom: 0px;
}
button.panel_icon {
border-radius: 12px;
transition: 100ms;
padding: 4px;
border-color: transparent;
background: transparent;
outline-color: transparent;
}
button.panel_icon:hover {
border-radius: 12px;
transition: 100ms;
padding: 4px;
border-color: rgba(255, 255, 255, 0.1);
outline-color: rgba(255, 255, 255, 0.1);
background: rgba(255, 255, 255, 0.1);
}
window.root_window {
background: transparent;
}

View file

@ -18,7 +18,7 @@ use gtk4::{
use std::{cell::RefCell, net::IpAddr, rc::Rc};
use zbus::Connection;
pub fn add_current_networks(target: &gtk4::Box, icon_image: &gtk4::Image) {
pub fn add_current_networks(target: &gtk4::Box, icon_image: &libcosmic_applet::AppletButton) {
let networks_list = ListBox::builder().show_separators(true).build();
let entries = Rc::<RefCell<Vec<ListBoxRow>>>::default();
let (tx, rx) = MainContext::channel::<Vec<ActiveConnectionInfo>>(PRIORITY_DEFAULT);
@ -38,7 +38,7 @@ fn display_active_connections(
connections: Vec<ActiveConnectionInfo>,
target: &ListBox,
entries: &mut Vec<ListBoxRow>,
icon_image: &gtk4::Image,
icon_image: &libcosmic_applet::AppletButton,
) {
for old_entry in entries.drain(..) {
target.remove(&old_entry);
@ -51,7 +51,7 @@ fn display_active_connections(
speed,
ip_addresses,
} => {
icon_image.set_icon_name(Some("network-wired-symbolic"));
icon_image.set_button_icon_name("network-wired-symbolic");
render_wired_connection(name, speed, ip_addresses)
}
ActiveConnectionInfo::WiFi {
@ -62,7 +62,7 @@ fn display_active_connections(
wpa_flags,
} => continue,
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
icon_image.set_icon_name(Some("network-vpn-symbolic"));
icon_image.set_button_icon_name("network-vpn-symbolic");
render_vpn(name, ip_addresses)
}
};