feat: better header centering

use new header function for Proxy and Node tab
This commit is contained in:
Cyrix126 2024-12-13 12:10:12 +01:00
parent 07ac351764
commit 525c92e2cf
3 changed files with 77 additions and 64 deletions

View file

@ -1,7 +1,12 @@
use egui::{Hyperlink, Image, Separator, TextStyle, TextWrapMode, Ui}; use egui::{Hyperlink, Image, Label, Separator, TextStyle, TextWrapMode, Ui};
use log::debug;
use crate::SPACE; use crate::SPACE;
// prevent compiling if no elements are added to a header. No need for a header then.
const fn check_header_element(is_logo_none: bool, is_links_empty: bool, is_subtitle_none: bool) {
if is_logo_none && is_links_empty && is_subtitle_none {
panic!("header_tab must be used with at least one element");
}
}
/// logo first, first hyperlink will be the header, description under. /// logo first, first hyperlink will be the header, description under.
/// will take care of centering widgets if boerder weight is more than 0. /// will take care of centering widgets if boerder weight is more than 0.
#[allow(clippy::needless_range_loop)] #[allow(clippy::needless_range_loop)]
@ -10,58 +15,66 @@ pub fn header_tab(
logo: Option<Image>, logo: Option<Image>,
// text, link, hover text // text, link, hover text
links: &[(&str, &str, &str)], links: &[(&str, &str, &str)],
subtitle: Option<String>, subtitle: Option<&str>,
one_line_center: bool, one_line_center: bool,
) { ) {
// width - logo and links and separators divided by double the size of logo (can't know width of links). check_header_element(logo.is_none(), links.is_empty(), subtitle.is_none());
ui.style_mut().wrap_mode = Some(TextWrapMode::Extend); ui.style_mut().wrap_mode = Some(TextWrapMode::Extend);
ui.style_mut().override_text_style = Some(TextStyle::Heading); ui.style_mut().override_text_style = Some(TextStyle::Heading);
ui.add_space(SPACE); ui.add_space(SPACE);
if one_line_center { if one_line_center {
let height = 64.0; ui.spacing_mut().item_spacing.x = 0.0;
let nb_links = links.len(); let height_logo = 64.0;
let border_weight = ((ui.available_width() let width_links = links
- ((height * 4.0 * nb_links as f32) + if logo.is_some() { height * 2.0 } else { 0.0 })) .iter()
/ (height * 2.0)) .map(|x| x.0.len() as f32 * ui.text_style_height(&TextStyle::Heading) / 2.0)
.max(0.0) as usize; .collect::<Vec<f32>>();
// nb_columns add logo if exist plus number of links with separator for each + number of column for border space let width_subtitle = subtitle.unwrap_or_default().len() as f32
let nb_columns = if logo.is_some() { 1 } else { 0 } + (links.len() * 2) + border_weight * 2; * ui.text_style_height(&TextStyle::Body)
ui.columns(nb_columns, |col| { / 2.0;
// first column for left border let nb_txt = links.len() + if subtitle.is_some() { 1 } else { 0 };
for n in 0..(border_weight) { // width of separator depends of width of ui and number of texts
col[n].vertical_centered(|ui| ui.add_space(0.0)); let width_separator = (ui.available_width() / ui.text_style_height(&TextStyle::Heading)
debug!("left side space: {}", n); * 4.0)
/ nb_txt as f32;
// width available - logo and separator - total width of txt - separator for each text then divided by two
let border_width = (((ui.available_width()
- if logo.is_some() {
height_logo + width_separator
} else {
0.0
} }
// jump first column, stop less 2, because begin at 0 and end with space column. - width_links.iter().sum::<f32>()
let mut nb_col = border_weight; - width_subtitle
- (nb_txt as f32 * width_separator))
+ width_separator)
/ 2.0)
.max(0.0);
ui.horizontal(|ui| {
ui.add_space(border_width);
if let Some(logo) = logo { if let Some(logo) = logo {
debug!("logo: {}", nb_col); ui.add_sized([height_logo, height_logo], logo);
col[nb_col].vertical_centered(|ui| ui.add_sized([height, height], logo)); ui.add_sized(
nb_col += 1; [width_separator, height_logo],
Separator::default().vertical().spacing(width_separator),
);
} }
for link in links { for (count, link) in links.iter().enumerate() {
debug!("separator: {}", nb_col);
col[nb_col].vertical_centered(|ui| {
ui.add_sized( ui.add_sized(
[height / 8.0, height], // [width_links[count], height_logo],
Separator::default().vertical().spacing(height / 8.0), [0.0, height_logo],
)
});
nb_col += 1;
debug!("link: {}", nb_col);
col[nb_col].vertical_centered(|ui| {
ui.add_sized(
[ui.available_width(), height],
Hyperlink::from_label_and_url(link.0, link.1), Hyperlink::from_label_and_url(link.0, link.1),
); );
}); if count != (links.len() - 1) || subtitle.is_some() {
nb_col += 1; ui.add_sized(
[0.0, height_logo],
Separator::default().vertical().spacing(width_separator),
);
} }
}
for n in nb_col..(nb_col + border_weight) { if let Some(desc) = subtitle {
debug!("right side border space: {}", n); ui.style_mut().override_text_style = Some(TextStyle::Body);
col[n].vertical_centered(|ui| ui.add_space(0.0)); ui.add_sized([0.0, height_logo], Label::new(desc));
} }
}); });
} else { } else {
@ -73,11 +86,11 @@ pub fn header_tab(
for link in links { for link in links {
ui.hyperlink_to(link.0, link.1); ui.hyperlink_to(link.0, link.1);
} }
ui.style_mut().override_text_style = Some(TextStyle::Body);
});
}
if let Some(desc) = subtitle { if let Some(desc) = subtitle {
ui.style_mut().override_text_style = Some(TextStyle::Body);
ui.label(desc); ui.label(desc);
} }
});
}
ui.add_space(SPACE); ui.add_space(SPACE);
} }

View file

@ -1,10 +1,11 @@
use crate::app::panels::middle::common::console::{console, input_args_field, start_options_field}; use crate::app::panels::middle::common::console::{console, input_args_field, start_options_field};
use crate::app::panels::middle::common::header_tab::header_tab;
use crate::app::panels::middle::common::state_edit_field::{path_db_field, slider_state_field}; use crate::app::panels::middle::common::state_edit_field::{path_db_field, slider_state_field};
use crate::app::panels::middle::{rpc_bind_field, rpc_port_field, zmq_bind_field, zmq_port_field}; use crate::app::panels::middle::{rpc_bind_field, rpc_port_field, zmq_bind_field, zmq_port_field};
use crate::{ use crate::{
NODE_ARGUMENTS, NODE_DNS_BLOCKLIST, NODE_DNS_CHECKPOINT, NODE_INPUT, NODE_PRUNNING, NODE_URL, NODE_ARGUMENTS, NODE_DNS_BLOCKLIST, NODE_DNS_CHECKPOINT, NODE_INPUT, NODE_PRUNNING, NODE_URL,
}; };
use egui::{Label, TextStyle}; use egui::TextStyle;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use log::debug; use log::debug;
@ -26,14 +27,13 @@ impl Node {
ui: &mut egui::Ui, ui: &mut egui::Ui,
) { ) {
ui.style_mut().override_text_style = Some(TextStyle::Body); ui.style_mut().override_text_style = Some(TextStyle::Body);
ui.vertical_centered(|ui| { header_tab(
ui.add_space(SPACE); ui,
ui.style_mut().override_text_style = Some(TextStyle::Heading); None,
ui.hyperlink_to("Monerod", NODE_URL); &[("Monerod", NODE_URL, "")],
ui.style_mut().override_text_style = None; Some("C++ Monero Node"),
ui.add(Label::new("C++ Monero Node")); true,
ui.add_space(SPACE); );
});
// console output for log // console output for log
debug!("Node Tab | Rendering [Console]"); debug!("Node Tab | Rendering [Console]");
egui::ScrollArea::vertical().show(ui, |ui| { egui::ScrollArea::vertical().show(ui, |ui| {

View file

@ -1,9 +1,10 @@
use egui::{Checkbox, Label, TextStyle, Ui, vec2}; use egui::{Checkbox, TextStyle, Ui, vec2};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use log::debug; use log::debug;
use crate::app::panels::middle::common::console::{console, input_args_field, start_options_field}; use crate::app::panels::middle::common::console::{console, input_args_field, start_options_field};
use crate::app::panels::middle::common::header_tab::header_tab;
use crate::app::panels::middle::common::list_poolnode::list_poolnode; use crate::app::panels::middle::common::list_poolnode::list_poolnode;
use crate::disk::state::XmrigProxy; use crate::disk::state::XmrigProxy;
use crate::helper::Process; use crate::helper::Process;
@ -29,14 +30,13 @@ impl XmrigProxy {
buffer: &mut String, buffer: &mut String,
ui: &mut egui::Ui, ui: &mut egui::Ui,
) { ) {
ui.vertical_centered(|ui| { header_tab(
ui.add_space(SPACE); ui,
ui.style_mut().override_text_style = Some(TextStyle::Heading); None,
ui.hyperlink_to("XMRig-Proxy", XMRIG_PROXY_URL); &[("XMRig-Proxy", XMRIG_PROXY_URL, "")],
ui.style_mut().override_text_style = Some(TextStyle::Body); Some("High performant proxy for your miners"),
ui.add(Label::new("High performant proxy for your miners")); true,
ui.add_space(SPACE); );
});
// console output for log // console output for log
debug!("Xmrig-Proxy Tab | Rendering [Console]"); debug!("Xmrig-Proxy Tab | Rendering [Console]");
egui::ScrollArea::vertical().show(ui, |ui| { egui::ScrollArea::vertical().show(ui, |ui| {