mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-04-25 01:58:51 +02:00
refactoring widget spacing
This commit is contained in:
@@ -475,13 +475,8 @@ impl eframe::App for Komobar {
|
|||||||
render_conf.alignment = Some(Alignment::Left);
|
render_conf.alignment = Some(Alignment::Left);
|
||||||
|
|
||||||
render_config.apply_on_alignment(ui, |ui| {
|
render_config.apply_on_alignment(ui, |ui| {
|
||||||
if let Some((last, rest)) = self.left_widgets.split_last_mut() {
|
for w in &mut self.left_widgets {
|
||||||
for w in rest.iter_mut() {
|
w.render(ctx, ui, &mut render_conf);
|
||||||
w.render(ctx, ui, render_conf);
|
|
||||||
}
|
|
||||||
|
|
||||||
render_conf.is_last_in_alignment = true;
|
|
||||||
last.render(ctx, ui, render_conf);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -492,13 +487,8 @@ impl eframe::App for Komobar {
|
|||||||
render_conf.alignment = Some(Alignment::Right);
|
render_conf.alignment = Some(Alignment::Right);
|
||||||
|
|
||||||
render_config.apply_on_alignment(ui, |ui| {
|
render_config.apply_on_alignment(ui, |ui| {
|
||||||
if let Some((last, rest)) = self.right_widgets.split_last_mut() {
|
for w in &mut self.right_widgets {
|
||||||
for w in rest.iter_mut() {
|
w.render(ctx, ui, &mut render_conf);
|
||||||
w.render(ctx, ui, render_conf);
|
|
||||||
}
|
|
||||||
|
|
||||||
render_conf.is_last_in_alignment = true;
|
|
||||||
last.render(ctx, ui, render_conf);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ impl Battery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Battery {
|
impl BarWidget for Battery {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
if self.enable {
|
if self.enable {
|
||||||
let output = self.output();
|
let output = self.output();
|
||||||
if !output.is_empty() {
|
if !output.is_empty() {
|
||||||
@@ -147,7 +147,7 @@ impl BarWidget for Battery {
|
|||||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||||
);
|
);
|
||||||
|
|
||||||
config.apply_on_widget(true, true, ui, |ui| {
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
ui.add(
|
ui.add(
|
||||||
Label::new(layout_job)
|
Label::new(layout_job)
|
||||||
.selectable(false)
|
.selectable(false)
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ impl Cpu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Cpu {
|
impl BarWidget for Cpu {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
if self.enable {
|
if self.enable {
|
||||||
let output = self.output();
|
let output = self.output();
|
||||||
if !output.is_empty() {
|
if !output.is_empty() {
|
||||||
@@ -99,7 +99,7 @@ impl BarWidget for Cpu {
|
|||||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||||
);
|
);
|
||||||
|
|
||||||
config.apply_on_widget(true, true, ui, |ui| {
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
if ui
|
if ui
|
||||||
.add(
|
.add(
|
||||||
Label::new(layout_job)
|
Label::new(layout_job)
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ impl Date {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Date {
|
impl BarWidget for Date {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
if self.enable {
|
if self.enable {
|
||||||
let mut output = self.output();
|
let mut output = self.output();
|
||||||
if !output.is_empty() {
|
if !output.is_empty() {
|
||||||
@@ -119,7 +119,7 @@ impl BarWidget for Date {
|
|||||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||||
);
|
);
|
||||||
|
|
||||||
config.apply_on_widget(true, true, ui, |ui| {
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
if ui
|
if ui
|
||||||
.add(
|
.add(
|
||||||
Label::new(WidgetText::LayoutJob(layout_job.clone()))
|
Label::new(WidgetText::LayoutJob(layout_job.clone()))
|
||||||
|
|||||||
@@ -122,32 +122,14 @@ pub struct Komorebi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Komorebi {
|
impl BarWidget for Komorebi {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
let mut komorebi_notification_state = self.komorebi_notification_state.borrow_mut();
|
let mut komorebi_notification_state = self.komorebi_notification_state.borrow_mut();
|
||||||
let mut add_to_ui: [bool; 4] = [self.workspaces.enable, false, false, false];
|
|
||||||
|
|
||||||
if let Some(layout) = self.layout {
|
if self.workspaces.enable {
|
||||||
add_to_ui[1] = layout.enable;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(configuration_switcher) = &self.configuration_switcher {
|
|
||||||
add_to_ui[2] = configuration_switcher.enable;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(focused_window) = self.focused_window {
|
|
||||||
if focused_window.enable {
|
|
||||||
let titles = &komorebi_notification_state.focused_container_information.0;
|
|
||||||
add_to_ui[3] = !titles.is_empty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let last_add_to_ui = add_to_ui.iter().rposition(|&x| x);
|
|
||||||
|
|
||||||
if add_to_ui[0] {
|
|
||||||
let mut update = None;
|
let mut update = None;
|
||||||
|
|
||||||
// NOTE: There should always be at least one workspace.
|
// NOTE: There should always be at least one workspace if the bar is connected to komorebi.
|
||||||
config.apply_on_widget(false, last_add_to_ui == Some(0), ui, |ui| {
|
config.apply_on_widget(false, ui, |ui| {
|
||||||
for (i, (ws, should_show)) in
|
for (i, (ws, should_show)) in
|
||||||
komorebi_notification_state.workspaces.iter().enumerate()
|
komorebi_notification_state.workspaces.iter().enumerate()
|
||||||
{
|
{
|
||||||
@@ -212,49 +194,60 @@ impl BarWidget for Komorebi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if add_to_ui[1] {
|
if let Some(layout) = self.layout {
|
||||||
config.apply_on_widget(true, last_add_to_ui == Some(1), ui, |ui| {
|
if layout.enable {
|
||||||
if ui
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
.add(
|
if ui
|
||||||
Label::new(komorebi_notification_state.layout.to_string())
|
.add(
|
||||||
.selectable(false)
|
Label::new(komorebi_notification_state.layout.to_string())
|
||||||
.sense(Sense::click()),
|
.selectable(false)
|
||||||
)
|
.sense(Sense::click()),
|
||||||
.clicked()
|
)
|
||||||
{
|
.clicked()
|
||||||
match komorebi_notification_state.layout {
|
{
|
||||||
KomorebiLayout::Default(_) => {
|
match komorebi_notification_state.layout {
|
||||||
if komorebi_client::send_message(&SocketMessage::CycleLayout(
|
KomorebiLayout::Default(_) => {
|
||||||
CycleDirection::Next,
|
if komorebi_client::send_message(&SocketMessage::CycleLayout(
|
||||||
))
|
CycleDirection::Next,
|
||||||
.is_err()
|
))
|
||||||
{
|
.is_err()
|
||||||
tracing::error!("could not send message to komorebi: CycleLayout");
|
{
|
||||||
|
tracing::error!(
|
||||||
|
"could not send message to komorebi: CycleLayout"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
KomorebiLayout::Floating => {
|
||||||
KomorebiLayout::Floating => {
|
if komorebi_client::send_message(&SocketMessage::ToggleTiling)
|
||||||
if komorebi_client::send_message(&SocketMessage::ToggleTiling).is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
tracing::error!("could not send message to komorebi: ToggleTiling");
|
tracing::error!(
|
||||||
|
"could not send message to komorebi: ToggleTiling"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
KomorebiLayout::Paused => {
|
||||||
KomorebiLayout::Paused => {
|
if komorebi_client::send_message(&SocketMessage::TogglePause)
|
||||||
if komorebi_client::send_message(&SocketMessage::TogglePause).is_err() {
|
.is_err()
|
||||||
tracing::error!("could not send message to komorebi: TogglePause");
|
{
|
||||||
|
tracing::error!(
|
||||||
|
"could not send message to komorebi: TogglePause"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
KomorebiLayout::Custom => {}
|
||||||
}
|
}
|
||||||
KomorebiLayout::Custom => {}
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if add_to_ui[2] {
|
if let Some(configuration_switcher) = &self.configuration_switcher {
|
||||||
let configuration_switcher = self.configuration_switcher.as_ref().unwrap();
|
if configuration_switcher.enable {
|
||||||
for (name, location) in configuration_switcher.configurations.iter() {
|
for (name, location) in configuration_switcher.configurations.iter() {
|
||||||
let path = PathBuf::from(location);
|
let path = PathBuf::from(location);
|
||||||
if path.is_file() {
|
if path.is_file() {
|
||||||
config.apply_on_widget(true, last_add_to_ui == Some(2), ui,|ui|{
|
config.apply_on_widget(true, ui,|ui|{
|
||||||
if ui
|
if ui
|
||||||
.add(Label::new(name).selectable(false).sense(Sense::click()))
|
.add(Label::new(name).selectable(false).sense(Sense::click()))
|
||||||
.clicked()
|
.clicked()
|
||||||
@@ -302,117 +295,125 @@ impl BarWidget for Komorebi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}});
|
}});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if add_to_ui[3] {
|
if let Some(focused_window) = self.focused_window {
|
||||||
let focused_window = self.focused_window.unwrap();
|
if focused_window.enable {
|
||||||
let titles = &komorebi_notification_state.focused_container_information.0;
|
let titles = &komorebi_notification_state.focused_container_information.0;
|
||||||
config.apply_on_widget(true, last_add_to_ui == Some(3), ui, |ui| {
|
if !titles.is_empty() {
|
||||||
let icons = &komorebi_notification_state.focused_container_information.1;
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
let focused_window_idx =
|
let icons = &komorebi_notification_state.focused_container_information.1;
|
||||||
komorebi_notification_state.focused_container_information.2;
|
let focused_window_idx =
|
||||||
|
komorebi_notification_state.focused_container_information.2;
|
||||||
|
|
||||||
let iter = titles.iter().zip(icons.iter());
|
let iter = titles.iter().zip(icons.iter());
|
||||||
|
|
||||||
for (i, (title, icon)) in iter.enumerate() {
|
for (i, (title, icon)) in iter.enumerate() {
|
||||||
if focused_window.show_icon {
|
if focused_window.show_icon {
|
||||||
if let Some(img) = icon {
|
if let Some(img) = icon {
|
||||||
ui.add(
|
ui.add(
|
||||||
Image::from(&img_to_texture(ctx, img))
|
Image::from(&img_to_texture(ctx, img))
|
||||||
.maintain_aspect_ratio(true)
|
.maintain_aspect_ratio(true)
|
||||||
.max_height(15.0),
|
.max_height(15.0),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if i == focused_window_idx {
|
|
||||||
let font_id = ctx
|
|
||||||
.style()
|
|
||||||
.text_styles
|
|
||||||
.get(&TextStyle::Body)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_else(FontId::default);
|
|
||||||
|
|
||||||
let layout_job = LayoutJob::simple(
|
|
||||||
title.to_string(),
|
|
||||||
font_id.clone(),
|
|
||||||
komorebi_notification_state
|
|
||||||
.stack_accent
|
|
||||||
.unwrap_or(ctx.style().visuals.selection.stroke.color),
|
|
||||||
100.0,
|
|
||||||
);
|
|
||||||
|
|
||||||
if titles.len() > 1 {
|
|
||||||
let available_height = ui.available_height();
|
|
||||||
let mut custom_ui = CustomUi(ui);
|
|
||||||
custom_ui.add_sized_left_to_right(
|
|
||||||
Vec2::new(
|
|
||||||
MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32,
|
|
||||||
available_height,
|
|
||||||
),
|
|
||||||
Label::new(layout_job).selectable(false).truncate(),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
let available_height = ui.available_height();
|
|
||||||
let mut custom_ui = CustomUi(ui);
|
|
||||||
custom_ui.add_sized_left_to_right(
|
|
||||||
Vec2::new(
|
|
||||||
MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32,
|
|
||||||
available_height,
|
|
||||||
),
|
|
||||||
Label::new(title).selectable(false).truncate(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let available_height = ui.available_height();
|
|
||||||
let mut custom_ui = CustomUi(ui);
|
|
||||||
|
|
||||||
if custom_ui
|
|
||||||
.add_sized_left_to_right(
|
|
||||||
Vec2::new(
|
|
||||||
MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32,
|
|
||||||
available_height,
|
|
||||||
),
|
|
||||||
Label::new(title)
|
|
||||||
.selectable(false)
|
|
||||||
.sense(Sense::click())
|
|
||||||
.truncate(),
|
|
||||||
)
|
|
||||||
.clicked()
|
|
||||||
{
|
|
||||||
if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(
|
|
||||||
false,
|
|
||||||
))
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
tracing::error!(
|
|
||||||
"could not send message to komorebi: MouseFollowsFocus"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if komorebi_client::send_message(&SocketMessage::FocusStackWindow(i))
|
if i == focused_window_idx {
|
||||||
.is_err()
|
let font_id = ctx
|
||||||
{
|
.style()
|
||||||
tracing::error!(
|
.text_styles
|
||||||
"could not send message to komorebi: FocusStackWindow"
|
.get(&TextStyle::Body)
|
||||||
);
|
.cloned()
|
||||||
}
|
.unwrap_or_else(FontId::default);
|
||||||
|
|
||||||
if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(
|
let layout_job = LayoutJob::simple(
|
||||||
komorebi_notification_state.mouse_follows_focus,
|
title.to_string(),
|
||||||
))
|
font_id.clone(),
|
||||||
.is_err()
|
komorebi_notification_state
|
||||||
{
|
.stack_accent
|
||||||
tracing::error!(
|
.unwrap_or(ctx.style().visuals.selection.stroke.color),
|
||||||
"could not send message to komorebi: MouseFollowsFocus"
|
100.0,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if titles.len() > 1 {
|
||||||
|
let available_height = ui.available_height();
|
||||||
|
let mut custom_ui = CustomUi(ui);
|
||||||
|
custom_ui.add_sized_left_to_right(
|
||||||
|
Vec2::new(
|
||||||
|
MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32,
|
||||||
|
available_height,
|
||||||
|
),
|
||||||
|
Label::new(layout_job).selectable(false).truncate(),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
let available_height = ui.available_height();
|
||||||
|
let mut custom_ui = CustomUi(ui);
|
||||||
|
custom_ui.add_sized_left_to_right(
|
||||||
|
Vec2::new(
|
||||||
|
MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32,
|
||||||
|
available_height,
|
||||||
|
),
|
||||||
|
Label::new(title).selectable(false).truncate(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let available_height = ui.available_height();
|
||||||
|
let mut custom_ui = CustomUi(ui);
|
||||||
|
|
||||||
|
if custom_ui
|
||||||
|
.add_sized_left_to_right(
|
||||||
|
Vec2::new(
|
||||||
|
MAX_LABEL_WIDTH.load(Ordering::SeqCst) as f32,
|
||||||
|
available_height,
|
||||||
|
),
|
||||||
|
Label::new(title)
|
||||||
|
.selectable(false)
|
||||||
|
.sense(Sense::click())
|
||||||
|
.truncate(),
|
||||||
|
)
|
||||||
|
.clicked()
|
||||||
|
{
|
||||||
|
if komorebi_client::send_message(
|
||||||
|
&SocketMessage::MouseFollowsFocus(false),
|
||||||
|
)
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
tracing::error!(
|
||||||
|
"could not send message to komorebi: MouseFollowsFocus"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if komorebi_client::send_message(
|
||||||
|
&SocketMessage::FocusStackWindow(i),
|
||||||
|
)
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
tracing::error!(
|
||||||
|
"could not send message to komorebi: FocusStackWindow"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if komorebi_client::send_message(
|
||||||
|
&SocketMessage::MouseFollowsFocus(
|
||||||
|
komorebi_notification_state.mouse_follows_focus,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
tracing::error!(
|
||||||
|
"could not send message to komorebi: MouseFollowsFocus"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ impl Media {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Media {
|
impl BarWidget for Media {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
if self.enable {
|
if self.enable {
|
||||||
let output = self.output();
|
let output = self.output();
|
||||||
if !output.is_empty() {
|
if !output.is_empty() {
|
||||||
@@ -102,7 +102,7 @@ impl BarWidget for Media {
|
|||||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||||
);
|
);
|
||||||
|
|
||||||
config.apply_on_widget(true, true, ui, |ui| {
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
let available_height = ui.available_height();
|
let available_height = ui.available_height();
|
||||||
let mut custom_ui = CustomUi(ui);
|
let mut custom_ui = CustomUi(ui);
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ impl Memory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Memory {
|
impl BarWidget for Memory {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
if self.enable {
|
if self.enable {
|
||||||
let output = self.output();
|
let output = self.output();
|
||||||
if !output.is_empty() {
|
if !output.is_empty() {
|
||||||
@@ -102,7 +102,7 @@ impl BarWidget for Memory {
|
|||||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||||
);
|
);
|
||||||
|
|
||||||
config.apply_on_widget(true, true, ui, |ui| {
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
if ui
|
if ui
|
||||||
.add(
|
.add(
|
||||||
Label::new(layout_job)
|
Label::new(layout_job)
|
||||||
|
|||||||
@@ -317,83 +317,71 @@ impl Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Network {
|
impl BarWidget for Network {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
let mut add_to_ui: [bool; 3] = [
|
if self.show_total_data_transmitted {
|
||||||
self.show_total_data_transmitted,
|
for output in self.total_data_transmitted() {
|
||||||
self.show_network_activity,
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
false,
|
ui.add(Label::new(output).selectable(false));
|
||||||
];
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.show_network_activity {
|
||||||
|
for output in self.network_activity() {
|
||||||
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
|
ui.add(Label::new(output).selectable(false));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if self.enable {
|
if self.enable {
|
||||||
self.default_interface();
|
self.default_interface();
|
||||||
|
|
||||||
if !self.default_interface.is_empty() {
|
if !self.default_interface.is_empty() {
|
||||||
add_to_ui[2] = true;
|
let font_id = ctx
|
||||||
}
|
.style()
|
||||||
}
|
.text_styles
|
||||||
|
.get(&TextStyle::Body)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_else(FontId::default);
|
||||||
|
|
||||||
let last_add_to_ui = add_to_ui.iter().rposition(|&x| x);
|
let mut layout_job = LayoutJob::simple(
|
||||||
|
match self.label_prefix {
|
||||||
|
LabelPrefix::Icon | LabelPrefix::IconAndText => {
|
||||||
|
egui_phosphor::regular::WIFI_HIGH.to_string()
|
||||||
|
}
|
||||||
|
LabelPrefix::None | LabelPrefix::Text => String::new(),
|
||||||
|
},
|
||||||
|
font_id.clone(),
|
||||||
|
ctx.style().visuals.selection.stroke.color,
|
||||||
|
100.0,
|
||||||
|
);
|
||||||
|
|
||||||
if add_to_ui[0] {
|
if let LabelPrefix::Text | LabelPrefix::IconAndText = self.label_prefix {
|
||||||
for output in self.total_data_transmitted() {
|
self.default_interface.insert_str(0, "NET: ");
|
||||||
config.apply_on_widget(true, last_add_to_ui == Some(0), ui, |ui| {
|
|
||||||
ui.add(Label::new(output).selectable(false));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if add_to_ui[1] {
|
|
||||||
for output in self.network_activity() {
|
|
||||||
config.apply_on_widget(true, last_add_to_ui == Some(1), ui, |ui| {
|
|
||||||
ui.add(Label::new(output).selectable(false));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if add_to_ui[2] {
|
|
||||||
let font_id = ctx
|
|
||||||
.style()
|
|
||||||
.text_styles
|
|
||||||
.get(&TextStyle::Body)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_else(FontId::default);
|
|
||||||
|
|
||||||
let mut layout_job = LayoutJob::simple(
|
|
||||||
match self.label_prefix {
|
|
||||||
LabelPrefix::Icon | LabelPrefix::IconAndText => {
|
|
||||||
egui_phosphor::regular::WIFI_HIGH.to_string()
|
|
||||||
}
|
|
||||||
LabelPrefix::None | LabelPrefix::Text => String::new(),
|
|
||||||
},
|
|
||||||
font_id.clone(),
|
|
||||||
ctx.style().visuals.selection.stroke.color,
|
|
||||||
100.0,
|
|
||||||
);
|
|
||||||
|
|
||||||
if let LabelPrefix::Text | LabelPrefix::IconAndText = self.label_prefix {
|
|
||||||
self.default_interface.insert_str(0, "NET: ");
|
|
||||||
}
|
|
||||||
|
|
||||||
layout_job.append(
|
|
||||||
&self.default_interface,
|
|
||||||
10.0,
|
|
||||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
|
||||||
);
|
|
||||||
|
|
||||||
config.apply_on_widget(true, last_add_to_ui == Some(2), ui, |ui| {
|
|
||||||
if ui
|
|
||||||
.add(
|
|
||||||
Label::new(layout_job)
|
|
||||||
.selectable(false)
|
|
||||||
.sense(Sense::click()),
|
|
||||||
)
|
|
||||||
.clicked()
|
|
||||||
{
|
|
||||||
if let Err(error) = Command::new("cmd.exe").args(["/C", "ncpa"]).spawn() {
|
|
||||||
eprintln!("{}", error)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
layout_job.append(
|
||||||
|
&self.default_interface,
|
||||||
|
10.0,
|
||||||
|
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||||
|
);
|
||||||
|
|
||||||
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
|
if ui
|
||||||
|
.add(
|
||||||
|
Label::new(layout_job)
|
||||||
|
.selectable(false)
|
||||||
|
.sense(Sense::click()),
|
||||||
|
)
|
||||||
|
.clicked()
|
||||||
|
{
|
||||||
|
if let Err(error) = Command::new("cmd.exe").args(["/C", "ncpa"]).spawn() {
|
||||||
|
eprintln!("{}", error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,12 +35,10 @@ pub struct RenderConfig {
|
|||||||
pub background_color: Color32,
|
pub background_color: Color32,
|
||||||
/// Alignment of the widgets
|
/// Alignment of the widgets
|
||||||
pub alignment: Option<Alignment>,
|
pub alignment: Option<Alignment>,
|
||||||
/// True if this config belongs to the last widget in the alignment
|
|
||||||
pub is_last_in_alignment: bool,
|
|
||||||
/// Add more inner margin when adding a widget group
|
/// Add more inner margin when adding a widget group
|
||||||
pub more_inner_margin: bool,
|
pub more_inner_margin: bool,
|
||||||
/// Add no widget spacing
|
/// Set to true after the first time the apply_on_widget was called on an alignment
|
||||||
pub no_spacing: bool,
|
pub applied_on_widget: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait RenderExt {
|
pub trait RenderExt {
|
||||||
@@ -54,9 +52,8 @@ impl RenderExt for &KomobarConfig {
|
|||||||
grouping: self.grouping.unwrap_or(Grouping::None),
|
grouping: self.grouping.unwrap_or(Grouping::None),
|
||||||
background_color,
|
background_color,
|
||||||
alignment: None,
|
alignment: None,
|
||||||
is_last_in_alignment: false,
|
|
||||||
more_inner_margin: false,
|
more_inner_margin: false,
|
||||||
no_spacing: false,
|
applied_on_widget: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,9 +65,8 @@ impl RenderConfig {
|
|||||||
grouping: Grouping::None,
|
grouping: Grouping::None,
|
||||||
background_color: Color32::BLACK,
|
background_color: Color32::BLACK,
|
||||||
alignment: None,
|
alignment: None,
|
||||||
is_last_in_alignment: false,
|
|
||||||
more_inner_margin: false,
|
more_inner_margin: false,
|
||||||
no_spacing: false,
|
applied_on_widget: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +78,7 @@ impl RenderConfig {
|
|||||||
self.alignment = None;
|
self.alignment = None;
|
||||||
|
|
||||||
if let Grouping::Bar(config) = self.grouping {
|
if let Grouping::Bar(config) = self.grouping {
|
||||||
return self.define_group(config, ui, add_contents);
|
return self.define_group(None, config, ui, add_contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::fallback_group(ui, add_contents)
|
Self::fallback_group(ui, add_contents)
|
||||||
@@ -96,7 +92,7 @@ impl RenderConfig {
|
|||||||
self.alignment = None;
|
self.alignment = None;
|
||||||
|
|
||||||
if let Grouping::Alignment(config) = self.grouping {
|
if let Grouping::Alignment(config) = self.grouping {
|
||||||
return self.define_group(config, ui, add_contents);
|
return self.define_group(None, config, ui, add_contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::fallback_group(ui, add_contents)
|
Self::fallback_group(ui, add_contents)
|
||||||
@@ -105,20 +101,17 @@ impl RenderConfig {
|
|||||||
pub fn apply_on_widget<R>(
|
pub fn apply_on_widget<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
more_inner_margin: bool,
|
more_inner_margin: bool,
|
||||||
is_last_add_to_ui: bool,
|
|
||||||
ui: &mut Ui,
|
ui: &mut Ui,
|
||||||
add_contents: impl FnOnce(&mut Ui) -> R,
|
add_contents: impl FnOnce(&mut Ui) -> R,
|
||||||
) -> InnerResponse<R> {
|
) -> InnerResponse<R> {
|
||||||
// since a widget can call this multiple times, it is necessary to know the last time
|
|
||||||
// in order to add widget spacing correctly
|
|
||||||
self.more_inner_margin = more_inner_margin;
|
self.more_inner_margin = more_inner_margin;
|
||||||
self.no_spacing = self.is_last_in_alignment && is_last_add_to_ui;
|
let outer_margin = self.widget_outer_margin();
|
||||||
|
|
||||||
if let Grouping::Widget(config) = self.grouping {
|
if let Grouping::Widget(config) = self.grouping {
|
||||||
return self.define_group(config, ui, add_contents);
|
return self.define_group(Some(outer_margin), config, ui, add_contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.fallback_widget_group(ui, add_contents)
|
self.fallback_widget_group(Some(outer_margin), ui, add_contents)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fallback_group<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
|
fn fallback_group<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
|
||||||
@@ -130,11 +123,12 @@ impl RenderConfig {
|
|||||||
|
|
||||||
fn fallback_widget_group<R>(
|
fn fallback_widget_group<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
outer_margin: Option<Margin>,
|
||||||
ui: &mut Ui,
|
ui: &mut Ui,
|
||||||
add_contents: impl FnOnce(&mut Ui) -> R,
|
add_contents: impl FnOnce(&mut Ui) -> R,
|
||||||
) -> InnerResponse<R> {
|
) -> InnerResponse<R> {
|
||||||
Frame::none()
|
Frame::none()
|
||||||
.outer_margin(self.widget_outer_margin())
|
.outer_margin(outer_margin.unwrap_or(Margin::ZERO))
|
||||||
.inner_margin(match self.more_inner_margin {
|
.inner_margin(match self.more_inner_margin {
|
||||||
true => Margin::symmetric(5.0, 0.0),
|
true => Margin::symmetric(5.0, 0.0),
|
||||||
false => Margin::same(0.0),
|
false => Margin::same(0.0),
|
||||||
@@ -144,12 +138,13 @@ impl RenderConfig {
|
|||||||
|
|
||||||
fn define_group<R>(
|
fn define_group<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
outer_margin: Option<Margin>,
|
||||||
config: GroupingConfig,
|
config: GroupingConfig,
|
||||||
ui: &mut Ui,
|
ui: &mut Ui,
|
||||||
add_contents: impl FnOnce(&mut Ui) -> R,
|
add_contents: impl FnOnce(&mut Ui) -> R,
|
||||||
) -> InnerResponse<R> {
|
) -> InnerResponse<R> {
|
||||||
Frame::none()
|
Frame::none()
|
||||||
.outer_margin(self.widget_outer_margin())
|
.outer_margin(outer_margin.unwrap_or(Margin::ZERO))
|
||||||
.inner_margin(match self.more_inner_margin {
|
.inner_margin(match self.more_inner_margin {
|
||||||
true => Margin::symmetric(8.0, 3.0),
|
true => Margin::symmetric(8.0, 3.0),
|
||||||
false => Margin::symmetric(3.0, 3.0),
|
false => Margin::symmetric(3.0, 3.0),
|
||||||
@@ -179,31 +174,33 @@ impl RenderConfig {
|
|||||||
.show(ui, add_contents)
|
.show(ui, add_contents)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn widget_outer_margin(&self) -> Margin {
|
fn set_spacing(&mut self) {
|
||||||
|
self.applied_on_widget = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn widget_outer_margin(&mut self) -> Margin {
|
||||||
|
let spacing = if self.applied_on_widget {
|
||||||
|
self.spacing
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
|
||||||
|
if !self.applied_on_widget {
|
||||||
|
self.set_spacing();
|
||||||
|
}
|
||||||
|
|
||||||
Margin {
|
Margin {
|
||||||
left: match self.alignment {
|
left: match self.alignment {
|
||||||
Some(align) => match align {
|
Some(align) => match align {
|
||||||
Alignment::Left => 0.0,
|
Alignment::Left => spacing,
|
||||||
Alignment::Right => {
|
Alignment::Right => 0.0,
|
||||||
if self.no_spacing {
|
|
||||||
0.0
|
|
||||||
} else {
|
|
||||||
self.spacing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
None => 0.0,
|
None => 0.0,
|
||||||
},
|
},
|
||||||
right: match self.alignment {
|
right: match self.alignment {
|
||||||
Some(align) => match align {
|
Some(align) => match align {
|
||||||
Alignment::Left => {
|
Alignment::Left => 0.0,
|
||||||
if self.no_spacing {
|
Alignment::Right => spacing,
|
||||||
0.0
|
|
||||||
} else {
|
|
||||||
self.spacing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Alignment::Right => 0.0,
|
|
||||||
},
|
},
|
||||||
None => 0.0,
|
None => 0.0,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ impl Storage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Storage {
|
impl BarWidget for Storage {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
if self.enable {
|
if self.enable {
|
||||||
let font_id = ctx
|
let font_id = ctx
|
||||||
.style()
|
.style()
|
||||||
@@ -88,9 +88,7 @@ impl BarWidget for Storage {
|
|||||||
.cloned()
|
.cloned()
|
||||||
.unwrap_or_else(FontId::default);
|
.unwrap_or_else(FontId::default);
|
||||||
|
|
||||||
let last_index = self.output().len() - 1;
|
for output in self.output() {
|
||||||
|
|
||||||
for (index, output) in self.output().iter().enumerate() {
|
|
||||||
let mut layout_job = LayoutJob::simple(
|
let mut layout_job = LayoutJob::simple(
|
||||||
match self.label_prefix {
|
match self.label_prefix {
|
||||||
LabelPrefix::Icon | LabelPrefix::IconAndText => {
|
LabelPrefix::Icon | LabelPrefix::IconAndText => {
|
||||||
@@ -104,12 +102,12 @@ impl BarWidget for Storage {
|
|||||||
);
|
);
|
||||||
|
|
||||||
layout_job.append(
|
layout_job.append(
|
||||||
output,
|
&output,
|
||||||
10.0,
|
10.0,
|
||||||
TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()),
|
TextFormat::simple(font_id.clone(), ctx.style().visuals.text_color()),
|
||||||
);
|
);
|
||||||
|
|
||||||
config.apply_on_widget(true, index == last_index, ui, |ui| {
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
if ui
|
if ui
|
||||||
.add(
|
.add(
|
||||||
Label::new(layout_job)
|
Label::new(layout_job)
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ impl Time {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidget for Time {
|
impl BarWidget for Time {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, mut config: RenderConfig) {
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig) {
|
||||||
if self.enable {
|
if self.enable {
|
||||||
let mut output = self.output();
|
let mut output = self.output();
|
||||||
if !output.is_empty() {
|
if !output.is_empty() {
|
||||||
@@ -110,7 +110,7 @@ impl BarWidget for Time {
|
|||||||
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
|
||||||
);
|
);
|
||||||
|
|
||||||
config.apply_on_widget(true, true, ui, |ui| {
|
config.apply_on_widget(true, ui, |ui| {
|
||||||
if ui
|
if ui
|
||||||
.add(
|
.add(
|
||||||
Label::new(layout_job)
|
Label::new(layout_job)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ use serde::Deserialize;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
pub trait BarWidget {
|
pub trait BarWidget {
|
||||||
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: RenderConfig);
|
fn render(&mut self, ctx: &Context, ui: &mut Ui, config: &mut RenderConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
|
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
|
||||||
|
|||||||
Reference in New Issue
Block a user