Skip to content

Commit

Permalink
Added server option to use custom user CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikReider committed Dec 10, 2023
1 parent 86934ad commit 0291200
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/global_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub(crate) fn handle_application_args(
}
}
}
"style" => continue,
e => {
eprintln!("Unknown Variant Key: \"{}\"!...", e);
return (HandleLocalStatus::FAILURE, actions);
Expand Down
11 changes: 10 additions & 1 deletion src/server/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ impl SwayOSDApplication {
pub fn new(action_receiver: Receiver<(ArgTypes, String)>) -> Self {
let app = Application::new(Some(APPLICATION_NAME), ApplicationFlags::FLAGS_NONE);

app.add_main_option(
"style",
glib::Char::from('s' as u8),
OptionFlags::NONE,
OptionArg::String,
"Use a custom Stylesheet file instead of looking for one",
Some("<CSS FILE PATH>"),
);

app.add_main_option(
"top-margin",
glib::Char::from(0),
Expand All @@ -33,7 +42,7 @@ impl SwayOSDApplication {
"OSD margin from top edge (0.5 would be screen center). Default is {}",
*utils::TOP_MARGIN_DEFAULT
),
Some("from 0.0 to 1.0"),
Some("<from 0.0 to 1.0>"),
);

let osd_app = SwayOSDApplication {
Expand Down
15 changes: 14 additions & 1 deletion src/server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use gtk::{
traits::IconThemeExt,
CssProvider, IconTheme, StyleContext,
};
use std::env::args_os;
use std::future::pending;
use std::path::PathBuf;
use std::str::FromStr;
use utils::{get_system_css_path, user_style_path};
use zbus::{dbus_interface, ConnectionBuilder};
Expand Down Expand Up @@ -105,7 +107,18 @@ fn main() {
}

// Try loading the users CSS theme
if let Some(user_config_path) = user_style_path() {
let mut custom_user_css: Option<PathBuf> = None;
let mut args = args_os().into_iter();
while let Some(arg) = args.next() {
match arg.to_str() {
Some("-s") | Some("--style") => match args.next() {
Some(path) => custom_user_css = path.to_str().map(|s| PathBuf::from(s)),
_ => (),
},
_ => (),
}
}
if let Some(user_config_path) = user_style_path(custom_user_css) {
let user_provider = CssProvider::new();
user_provider
.load_from_path(&user_config_path)
Expand Down
1 change: 1 addition & 0 deletions src/server/osd_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl SwayosdWindow {
/// Create a new window and assign it to the given application.
pub fn new(app: &gtk::Application, display: &gdk::Display, monitor: &gdk::Monitor) -> Self {
let window = gtk::ApplicationWindow::new(app);
window.set_widget_name("osd");
window
.style_context()
.add_class(&gtk::STYLE_CLASS_OSD.to_string());
Expand Down
9 changes: 7 additions & 2 deletions src/server/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,13 @@ pub fn get_system_css_path() -> Option<PathBuf> {
path
}

pub fn user_style_path() -> Option<String> {
let path = user_config_dir().join("swayosd/style.css");
pub fn user_style_path(custom_path: Option<PathBuf>) -> Option<String> {
let path = user_config_dir().join("swayosd").join("style.css");
if let Some(custom_path) = custom_path {
if custom_path.exists() {
return custom_path.to_str().map(|s| s.to_string());
}
}
if path.exists() {
return path.to_str().map(|s| s.to_string());
}
Expand Down

0 comments on commit 0291200

Please sign in to comment.