Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/unstable' into unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedilger committed Sep 25, 2024
2 parents 3fc008b + e683ac0 commit 8c1fa67
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 176 deletions.
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/relays/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr
super::relay_sort_combo(app, ui);
btn_h_space!(ui);
widgets::TextEdit::search(&app.theme, &app.assets, &mut app.relays.search)
.desired_width(150.0)
.desired_width(super::SEARCH_WIDTH)
.show(ui);
if widgets::Button::primary(&app.theme, "Add Relay")
.show(ui)
Expand Down
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/relays/known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr
super::relay_sort_combo(app, ui);
btn_h_space!(ui);
widgets::TextEdit::search(&app.theme, &app.assets, &mut app.relays.search)
.desired_width(150.0)
.desired_width(super::SEARCH_WIDTH)
.show(ui);
if widgets::Button::primary(&app.theme, "Add Relay")
.show(ui)
Expand Down
25 changes: 2 additions & 23 deletions gossip-bin/src/ui/relays/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use crate::ui::{widgets, Page};
use eframe::egui;
use egui::{Context, Ui};
use egui_winit::egui::Id;
use gossip_lib::comms::ToOverlordMessage;
use gossip_lib::Relay;
use gossip_lib::GLOBALS;
use std::sync::atomic::Ordering;

pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui) {
let is_editing = app.relays.edit.is_some();
Expand All @@ -21,7 +19,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr
super::relay_sort_combo(app, ui);
btn_h_space!(ui);
widgets::TextEdit::search(&app.theme, &app.assets, &mut app.relays.search)
.desired_width(150.0)
.desired_width(super::SEARCH_WIDTH)
.show(ui);
if widgets::Button::primary(&app.theme, "Add Relay")
.show(ui)
Expand All @@ -30,26 +28,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr
super::start_entry_dialog(app);
}

let advertise_remaining = GLOBALS.advertise_jobs_remaining.load(Ordering::Relaxed);
if advertise_remaining == 0 {
if widgets::Button::secondary(&app.theme,"Advertise Relay List")
.show(ui)
.on_hover_text("Advertise my relays. Will send your relay usage information to every relay that seems to be working well so that other people know how to follow and contact you.")
.clicked()
{
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::AdvertiseRelayList);
}
} else {
ui.add_enabled(
false,
widgets::Button::secondary(
&app.theme,
format!("Advertising, {} to go", advertise_remaining),
),
);
}
// let advertise_remaining = GLOBALS.advertise_jobs_remaining.load(Ordering::Relaxed);
});

let relays = if !is_editing {
Expand Down
23 changes: 20 additions & 3 deletions gossip-bin/src/ui/relays/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod coverage;
mod known;
mod mine;

pub const SEARCH_WIDTH: f32 = 80.0;
pub const RELAY_URL_PREPOPULATE: &str = "wss://";

pub(super) struct RelayUi {
Expand Down Expand Up @@ -244,7 +245,9 @@ pub(super) fn relay_scroll_list(
if let Some(ref assignment) = GLOBALS.relay_picker.get_relay_assignment(&db_url) {
widget.set_user_count(assignment.pubkeys.len());
}
let response = ui.add_enabled(enabled, widget.clone());
let response = ui
.add_enabled_ui(enabled, |ui| widget.show(ui, &app.theme))
.inner;
if response.clicked() {
if !edit {
app.relays.edit = Some(db_url);
Expand Down Expand Up @@ -432,7 +435,7 @@ pub(super) fn configure_list_btn(app: &mut GossipUi, ui: &mut Ui) {
let max_size = vec2(180.0, ui.ctx().available_rect().height());

let text = egui::RichText::new("=").size(13.0);
let response = widgets::Button::primary(&app.theme, text)
let response = widgets::Button::secondary(&app.theme, text)
.small(true)
.show(ui);
let menu = widgets::MoreMenu::bubble(ui.next_auto_id(), min_size, max_size);
Expand Down Expand Up @@ -462,6 +465,20 @@ pub(super) fn configure_list_btn(app: &mut GossipUi, ui: &mut Ui) {
}),
)));

if app.page == Page::RelaysMine {
items.push(MoreMenuItem::Button(MoreMenuButton::new("Advertise Relay List",
Box::new(|_ui, _app| {
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::AdvertiseRelayList);

}))
.enabled(GLOBALS.identity.is_unlocked())
.on_disabled_hover_text("Add or unlock your private key to advertise your relays")
.on_hover_text("Advertise my relays. Will send your relay usage information to every relay that seems to be working well so that other people know how to follow and contact you.")
));
}

menu.show_entries(ui, app, response, items);
});
}
Expand Down Expand Up @@ -604,7 +621,7 @@ pub(super) fn sort_relay(rui: &RelayUi, a: &Relay, b: &Relay) -> Ordering {
.then(b.get_usage_bits_for_sorting().cmp(&a.get_usage_bits_for_sorting()))
.then(b.is_good_for_advertise().cmp(&a.is_good_for_advertise()))
.then(a.url.cmp(&b.url)),
RelaySorting::Name => a.url.cmp(&b.url),
RelaySorting::Name => a.url.host().cmp(&b.url.host()).then(a.url.cmp(&b.url)),
RelaySorting::WriteRelays => b.has_usage_bits(Relay::WRITE)
.cmp(&a.has_usage_bits(Relay::WRITE))
.then(a.url.cmp(&b.url)),
Expand Down
96 changes: 93 additions & 3 deletions gossip-bin/src/ui/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) mod list_entry;
pub use copy_button::{CopyButton, COPY_SYMBOL_SIZE};

mod nav_item;
use eframe::egui::{vec2, FontId, Galley, Rect};
use eframe::egui::{vec2, FontId, Galley, Pos2, Rect};
use egui_winit::egui::text::LayoutJob;
use egui_winit::egui::{
self, Align, FontSelection, Response, RichText, Rounding, Sense, Ui, WidgetText,
Expand Down Expand Up @@ -137,8 +137,98 @@ pub fn relay_url(ui: &mut Ui, theme: &Theme, url: &RelayUrl) -> Response {
font.size *= 0.7;

ui.painter().text(
response.rect.left_top(),
egui::Align2::CENTER_TOP,
response.rect.left_center(),
egui::Align2::CENTER_CENTER,
symbol,
font,
color,
);

response
}

pub fn relay_url_at(
ui: &mut Ui,
theme: &Theme,
pos: Pos2,
max_width: f32,
url: &RelayUrl,
font_size: Option<f32>,
with_path: bool,
) -> Response {
let (symbol, color, spacer) = if url.as_url_crate_url().scheme() != "wss" {
(
"\u{00A0}\u{00A0}\u{1F513}",
theme.red_500(),
"\u{00A0}\u{00A0}\u{00A0}",
)
} else {
("", theme.accent_color(), "")
};
let url = url.as_url_crate_url();
let text = format!("{}{}", spacer, url.host_str().unwrap_or_default());
let text = if let Some(size) = font_size {
RichText::new(text).size(size)
} else {
RichText::new(text)
};

let rect = if !with_path {
let galley = list_entry::text_to_galley_max_width(ui, text.into(), Align::LEFT, max_width);
let rect =
list_entry::draw_text_galley_at(ui, pos, galley, Some(theme.accent_color()), None);
rect
} else {
let galley = list_entry::text_to_galley_max_width(ui, text.into(), Align::LEFT, max_width);
let max_width = max_width - galley.rect.width();

let path_text = if let Some(host) = url.host_str() {
url.as_str()
.split_once(host)
.map_or(
None,
|(_before, after)| if after.len() > 1 { Some(after) } else { None },
)
} else {
url.as_str()
.split_once("/")
.map_or(None, |(_before, after)| Some(after))
};

let path_text = format!(
"\u{00A0}\u{00A0}{}",
path_text.unwrap_or_default().trim_end_matches('/')
);

let path_text = if let Some(size) = font_size {
RichText::new(path_text).size(size)
} else {
RichText::new(path_text)
};
let path_galley =
list_entry::text_to_galley_max_width(ui, path_text.into(), Align::LEFT, max_width);

let rect =
list_entry::draw_text_galley_at(ui, pos, galley, Some(theme.accent_color()), None);
let path_rect = list_entry::draw_text_galley_at(
ui,
pos + vec2(rect.width(), 0.0),
path_galley,
None,
None,
);

rect.union(path_rect)
};

let response = ui.interact(rect, ui.next_auto_id().with("rtitle"), Sense::hover());

let mut font = FontId::default();
font.size = font_size.unwrap_or(font.size) * 0.7;

ui.painter().text(
response.rect.left_center(),
egui::Align2::CENTER_CENTER,
symbol,
font,
color,
Expand Down
39 changes: 36 additions & 3 deletions gossip-bin/src/ui/widgets/more_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub(in crate::ui) enum MoreMenuItem<'a> {
#[allow(clippy::type_complexity)]
pub(in crate::ui) struct MoreMenuButton<'a> {
text: WidgetText,
on_hover_text: Option<WidgetText>,
on_disabled_hover_text: Option<WidgetText>,
action: Box<dyn FnOnce(&mut Ui, &mut GossipUi) + 'a>,
enabled: bool,
}
Expand All @@ -45,11 +47,26 @@ impl<'a> MoreMenuButton<'a> {
) -> Self {
Self {
text: text.into(),
on_hover_text: None,
on_disabled_hover_text: None,
action,
enabled: true,
}
}

/// Set an optional `on_hover_text`
pub fn on_hover_text(mut self, text: impl Into<WidgetText>) -> Self {
self.on_hover_text = Some(text.into());
self
}

/// Set an optional `on_disabled_hover_text`
pub fn on_disabled_hover_text(mut self, text: impl Into<WidgetText>) -> Self {
self.on_disabled_hover_text = Some(text.into());
self
}

/// Set `enabled` state of this button
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
Expand All @@ -71,7 +88,14 @@ impl<'a> MoreMenuButton<'a> {
ui.disable();
}

let response = draw_menu_button(ui, &app.theme, self.text, None);
let response = draw_menu_button(
ui,
&app.theme,
self.text,
None,
self.on_hover_text,
self.on_disabled_hover_text,
);

// process action
if response.clicked() {
Expand Down Expand Up @@ -141,7 +165,7 @@ impl<'a> MoreMenuSubMenu<'a> {

let mut open = load_state(ui, &self.id);

let response = draw_menu_button(ui, &app.theme, self.title, Some(open));
let response = draw_menu_button(ui, &app.theme, self.title, Some(open), None, None);

// TODO paint open/close arrow, use animation

Expand Down Expand Up @@ -779,12 +803,14 @@ fn draw_menu_button(
theme: &Theme,
title: WidgetText,
force_hover: Option<bool>,
on_hover_text: Option<WidgetText>,
on_disabled_hover_text: Option<WidgetText>,
) -> Response {
// layout
let desired_size = vec2(ui.available_width(), 32.0);

// interact
let (rect, response) = ui.allocate_at_least(desired_size, Sense::click());
let (rect, mut response) = ui.allocate_at_least(desired_size, Sense::click());
response.widget_info(|| WidgetInfo::labeled(WidgetType::Button, ui.is_enabled(), title.text()));
let state = super::interact_widget_state(ui, &response);
let state = match state {
Expand Down Expand Up @@ -902,5 +928,12 @@ fn draw_menu_button(
);
painter.add(shapes);

if let Some(text) = on_hover_text {
response = response.on_hover_text(text);
}
if let Some(text) = on_disabled_hover_text {
response = response.on_disabled_hover_text(text);
}

response
}
Loading

0 comments on commit 8c1fa67

Please sign in to comment.