513 lines
28 KiB
Rust
513 lines
28 KiB
Rust
//! Bars above the page (permission requests, save password, ...), the find bar, and dialogs in
|
|
//! front of the page (alerts, sign-in, select lists, colour picker, autofill).
|
|
|
|
use egui::{Align2, Id, Rect, RichText, Sense, Ui, Vec2, pos2, vec2};
|
|
|
|
use super::menus::{menu_item, surface};
|
|
use super::widgets::{ButtonKind, button, chip, icon, icon_button, password_field, text_field};
|
|
use super::{Frame, paint};
|
|
use crate::actions::{Command, DialogResult, PromptAnswer};
|
|
use crate::tabs::{Dialog, Prompt};
|
|
use crate::theme::{self, Palette, Weight};
|
|
use crate::ui::icons;
|
|
use crate::util;
|
|
|
|
/// A modal dialog: the page dims behind a scrim, and a card of `surface_container_high` with large corners and one
|
|
/// soft shadow eases in at the centre of the page. `body` draws title, text, fields and `dialog_actions`.
|
|
fn dialog_card(ctx: &egui::Context, p: &Palette, page: Rect, id: Id, width: f32, body: &mut dyn FnMut(&mut Ui)) {
|
|
// The card eases in from where it was last frame's start; a new dialog gets a new id so it starts fresh.
|
|
let seen = ctx.data(|d| d.get_temp::<bool>(id.with("seen"))).unwrap_or(false);
|
|
let t = if theme::tokens(ctx).motion <= 0.0 {
|
|
1.0
|
|
} else if seen {
|
|
theme::spring(ctx, id.with("in"), 1.0, theme::EFFECT)
|
|
} else {
|
|
ctx.data_mut(|d| d.insert_temp(id.with("seen"), true));
|
|
ctx.request_repaint();
|
|
theme::spring(ctx, id.with("in"), 0.0, theme::EFFECT)
|
|
}
|
|
.clamp(0.0, 1.0);
|
|
let scrim = ctx.layer_painter(egui::LayerId::new(egui::Order::Middle, id.with("scrim")));
|
|
scrim.rect_filled(page, 0.0, p.scrim.gamma_multiply(0.32 * t));
|
|
let radius = theme::radius(ctx, theme::XL);
|
|
let width = width.min(page.width() - 2.0 * theme::GAP_L).max(240.0);
|
|
egui::Area::new(id).fixed_pos(page.center() + vec2(0.0, (1.0 - t) * 16.0)).pivot(Align2::CENTER_CENTER).order(egui::Order::Foreground).show(ctx, |ui| {
|
|
ui.set_opacity(t);
|
|
egui::Frame::NONE
|
|
.fill(p.surface_container_high)
|
|
.corner_radius(theme::corner_radius(radius))
|
|
.inner_margin(egui::Margin::same(24))
|
|
.shadow(egui::Shadow { offset: [0, 6], blur: 24, spread: 0, color: egui::Color32::from_black_alpha(if p.dark { 150 } else { 70 }) })
|
|
.show(ui, |ui| {
|
|
ui.set_width(width - 48.0);
|
|
body(ui);
|
|
});
|
|
});
|
|
let _ = paint::shadow;
|
|
}
|
|
|
|
fn dialog_title(ui: &mut Ui, text: &str) {
|
|
let p = theme::palette(ui.ctx());
|
|
ui.label(RichText::new(text).font(theme::font(theme::HEADLINE, Weight::Medium)).color(p.on_surface));
|
|
ui.add_space(theme::GAP_M);
|
|
}
|
|
|
|
fn dialog_text(ui: &mut Ui, text: &str) {
|
|
let p = theme::palette(ui.ctx());
|
|
ui.label(RichText::new(text).font(theme::font(theme::BODY, Weight::Regular)).color(p.on_surface_variant));
|
|
}
|
|
|
|
/// The buttons at the bottom right, the main action last. Gets the row's layout (right to left): add the main
|
|
/// action first.
|
|
fn dialog_actions(ui: &mut Ui, contents: impl FnOnce(&mut Ui)) {
|
|
ui.add_space(theme::GAP_XL);
|
|
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
|
ui.spacing_mut().item_spacing.x = theme::GAP_S;
|
|
contents(ui);
|
|
});
|
|
}
|
|
|
|
pub fn prompt_bars(root: &mut Ui, f: &mut Frame) {
|
|
let Some(tab) = f.tab() else { return };
|
|
let Some(prompt) = tab.prompts.front() else { return };
|
|
let tab_id = tab.id;
|
|
let more = tab.prompts.len() - 1;
|
|
let text = match prompt {
|
|
Prompt::Permission { host, kind, .. } => format!("{host} wants to {}", kind.request_text()),
|
|
Prompt::ProtocolHandler { host, scheme, .. } => format!("{host} wants to open {scheme}: links"),
|
|
Prompt::SaveLogin { origin, username, update, .. } => {
|
|
let who = if username.is_empty() { String::new() } else { format!(" for {username}") };
|
|
if *update { format!("Update the saved password{who} on {}?", util::host_of_str(origin)) } else { format!("Save the password{who} on {}?", util::host_of_str(origin)) }
|
|
},
|
|
Prompt::HttpFallback { url } => format!("{} has no secure version. Open it over an unencrypted connection?", util::host_of(url)),
|
|
Prompt::External { url } => format!("Open this link in another app? {}", util::ellipsize(url, 60)),
|
|
Prompt::PopupBlocked { .. } => "A pop-up was blocked on this site".to_owned(),
|
|
Prompt::Info { text } => text.clone(),
|
|
};
|
|
let (kind, glyph) = match prompt {
|
|
Prompt::Permission { .. } | Prompt::ProtocolHandler { .. } => (0, icons::SHIELD),
|
|
Prompt::SaveLogin { .. } => (1, icons::KEY),
|
|
Prompt::HttpFallback { .. } => (2, icons::LOCK_OPEN),
|
|
Prompt::External { .. } => (3, icons::OPEN_IN_NEW),
|
|
Prompt::PopupBlocked { .. } => (4, icons::BLOCK),
|
|
Prompt::Info { .. } => (5, icons::INFO),
|
|
};
|
|
let p = f.palette();
|
|
let frame = egui::Frame::NONE.fill(p.surface_container_high).inner_margin(egui::Margin::symmetric(theme::GAP_L as i8, theme::GAP_S as i8));
|
|
egui::Panel::top("prompt-bar").frame(frame).show(root, |ui| {
|
|
ui.horizontal(|ui| {
|
|
ui.spacing_mut().item_spacing.x = theme::GAP_S;
|
|
ui.set_min_height(theme::CONTROL);
|
|
let (rect, _) = ui.allocate_exact_size(Vec2::splat(theme::ICON_LARGE), Sense::hover());
|
|
icon(ui, rect.center(), glyph, theme::ICON, false, p.primary);
|
|
ui.add_space(theme::GAP_XS);
|
|
let mut answer = None;
|
|
// Buttons first from the right; the text takes what is left and is cut with an ellipsis.
|
|
let mut buttons = ui.new_child(egui::UiBuilder::new().max_rect(ui.max_rect()).layout(egui::Layout::right_to_left(egui::Align::Center)));
|
|
buttons.spacing_mut().item_spacing.x = theme::GAP_S;
|
|
if icon_button(&mut buttons, icons::CLOSE, "Not now").clicked() {
|
|
answer = Some(PromptAnswer::Dismiss);
|
|
}
|
|
match kind {
|
|
0 | 3 => {
|
|
let remember_id = Id::new(("remember", tab_id));
|
|
let mut remember = buttons.data_mut(|d| *d.get_temp_mut_or(remember_id, true));
|
|
if button(&mut buttons, ButtonKind::Filled, None, "Allow").clicked() {
|
|
answer = Some(PromptAnswer::Allow { remember });
|
|
}
|
|
if button(&mut buttons, ButtonKind::Tonal, None, "Block").clicked() {
|
|
answer = Some(PromptAnswer::Deny { remember });
|
|
}
|
|
if chip(&mut buttons, if remember { Some(icons::CHECK) } else { None }, "Remember", remember).clicked() {
|
|
remember = !remember;
|
|
}
|
|
buttons.data_mut(|d| d.insert_temp(remember_id, remember));
|
|
},
|
|
1 => {
|
|
if button(&mut buttons, ButtonKind::Filled, None, "Save").clicked() {
|
|
answer = Some(PromptAnswer::Yes);
|
|
}
|
|
if button(&mut buttons, ButtonKind::Tonal, None, "Never for this site").clicked() {
|
|
answer = Some(PromptAnswer::Never);
|
|
}
|
|
},
|
|
2 => {
|
|
if button(&mut buttons, ButtonKind::Filled, None, "Open anyway").clicked() {
|
|
answer = Some(PromptAnswer::Yes);
|
|
}
|
|
if button(&mut buttons, ButtonKind::Tonal, None, "Always for this site").clicked() {
|
|
answer = Some(PromptAnswer::Allow { remember: true });
|
|
}
|
|
},
|
|
4 => {
|
|
if button(&mut buttons, ButtonKind::Tonal, None, "Always allow pop-ups here").clicked() {
|
|
answer = Some(PromptAnswer::Allow { remember: true });
|
|
}
|
|
},
|
|
_ => {},
|
|
}
|
|
let used = buttons.min_rect().width();
|
|
let mut label = text;
|
|
if more > 0 {
|
|
label = format!("{label} (+{more} more)");
|
|
}
|
|
let width = (ui.available_width() - used - theme::GAP_L).max(80.0);
|
|
let galley = super::menus::one_line(ui, &label, theme::font(theme::BODY, Weight::Medium), p.on_surface, width);
|
|
ui.add(egui::Label::new(galley));
|
|
if let Some(answer) = answer {
|
|
f.send(Command::Prompt { tab: tab_id, answer });
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
pub fn find_bar(root: &mut Ui, f: &mut Frame) {
|
|
let Some(tab) = f.tab() else { return };
|
|
if !tab.find.open {
|
|
return;
|
|
}
|
|
let mut find = tab.find.clone();
|
|
let p = f.palette();
|
|
let frame = egui::Frame::NONE.fill(p.surface_container_high).inner_margin(egui::Margin::symmetric(theme::GAP_L as i8, theme::GAP_S as i8));
|
|
egui::Panel::top("find-bar").frame(frame).show(root, |ui| {
|
|
ui.horizontal(|ui| {
|
|
ui.spacing_mut().item_spacing.x = theme::GAP_S;
|
|
ui.set_min_height(theme::CONTROL);
|
|
let response = text_field(ui, &mut find.text, "Find in page", 280.0);
|
|
if find.focus {
|
|
response.request_focus();
|
|
find.focus = false;
|
|
}
|
|
let enter = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
|
|
let shift = ui.input(|i| i.modifiers.shift);
|
|
if response.changed() {
|
|
f.send(Command::Find { text: find.text.clone(), forward: true, case: find.case_sensitive });
|
|
}
|
|
if enter {
|
|
f.send(Command::Find { text: find.text.clone(), forward: !shift, case: find.case_sensitive });
|
|
response.request_focus();
|
|
}
|
|
if response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Escape)) {
|
|
f.send(Command::FindClose);
|
|
}
|
|
let label = if find.text.is_empty() { String::new() } else if find.count == 0 { "No matches".into() } else { format!("{} of {}", find.index, find.count) };
|
|
ui.label(RichText::new(label).font(theme::font(theme::SMALL, Weight::Medium)).color(if !find.text.is_empty() && find.count == 0 { p.error } else { p.on_surface_variant }));
|
|
if icon_button(ui, icons::ARROW_UPWARD, "Previous (Shift+Enter)").clicked() {
|
|
f.send(Command::Find { text: find.text.clone(), forward: false, case: find.case_sensitive });
|
|
}
|
|
if icon_button(ui, icons::ARROW_DOWNWARD, "Next (Enter)").clicked() {
|
|
f.send(Command::Find { text: find.text.clone(), forward: true, case: find.case_sensitive });
|
|
}
|
|
if chip(ui, if find.case_sensitive { Some(icons::CHECK) } else { None }, "Match case", find.case_sensitive).clicked() {
|
|
find.case_sensitive = !find.case_sensitive;
|
|
f.send(Command::Find { text: find.text.clone(), forward: true, case: find.case_sensitive });
|
|
}
|
|
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
|
if icon_button(ui, icons::CLOSE, "Close").clicked() {
|
|
f.send(Command::FindClose);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
if let Some(tab) = f.tab_mut() {
|
|
tab.find.text = find.text;
|
|
tab.find.case_sensitive = find.case_sensitive;
|
|
tab.find.focus = find.focus;
|
|
}
|
|
}
|
|
|
|
/// Dialogs that belong to the page, drawn over it.
|
|
pub fn page_dialogs(ctx: &egui::Context, f: &mut Frame) {
|
|
let active = *f.active;
|
|
let Some(tab) = f.tabs.get_mut(active) else { return };
|
|
let tab_id = tab.id;
|
|
let Some(dialog) = tab.dialog.take() else { return };
|
|
let page = f.page_rect.unwrap_or_else(|| ctx.content_rect());
|
|
let palette = f.palette();
|
|
// Each dialog gets a new id so it eases in from the start.
|
|
let count: u32 = ctx.data(|d| d.get_temp(Id::new("dialog-count"))).unwrap_or(0);
|
|
let card = |ctx: &egui::Context, id: &str, width: f32, body: &mut dyn FnMut(&mut Ui)| {
|
|
dialog_card(ctx, &palette, page, Id::new((id, tab_id, count)), width, body);
|
|
};
|
|
let mut result: Option<DialogResult> = None;
|
|
let mut extra: Vec<Command> = Vec::new();
|
|
match &dialog {
|
|
Dialog::Simple(simple, origin) => {
|
|
let value_id = Id::new(("prompt-value", tab_id));
|
|
card(ctx, "simple-dialog", 400.0, &mut |ui| {
|
|
let host = util::host_of_str(origin);
|
|
dialog_title(ui, &if host.is_empty() { "This page says".to_owned() } else { format!("{host} says") });
|
|
egui::ScrollArea::vertical().max_height(240.0).auto_shrink([true, true]).show(ui, |ui| dialog_text(ui, &util::ellipsize(simple.message(), 2000)));
|
|
let mut value: Option<String> = None;
|
|
if let servo::SimpleDialog::Prompt(prompt) = simple {
|
|
ui.add_space(theme::GAP_L);
|
|
let mut text = ui.data_mut(|d| d.get_temp::<String>(value_id)).unwrap_or_else(|| prompt.current_value().to_owned());
|
|
let r = text_field(ui, &mut text, "", ui.available_width());
|
|
r.request_focus();
|
|
ui.data_mut(|d| d.insert_temp(value_id, text.clone()));
|
|
value = Some(text);
|
|
}
|
|
let alert = matches!(simple, servo::SimpleDialog::Alert(_));
|
|
dialog_actions(ui, |ui| {
|
|
if button(ui, ButtonKind::Filled, None, "OK").clicked() || ui.input(|i| i.key_pressed(egui::Key::Enter)) {
|
|
result = Some(DialogResult::Ok(value.clone()));
|
|
}
|
|
if !alert && button(ui, ButtonKind::Text, None, "Cancel").clicked() {
|
|
result = Some(DialogResult::Cancel);
|
|
}
|
|
});
|
|
if ui.input(|i| i.key_pressed(egui::Key::Escape)) {
|
|
result = Some(if alert { DialogResult::Ok(None) } else { DialogResult::Cancel });
|
|
}
|
|
});
|
|
if result.is_some() {
|
|
ctx.data_mut(|d| d.remove::<String>(value_id));
|
|
}
|
|
},
|
|
Dialog::Auth { request, username, password } => {
|
|
let id = Id::new(("auth", tab_id));
|
|
card(ctx, "auth-dialog", 400.0, &mut |ui| {
|
|
let host = request.url().host_str().unwrap_or("").to_owned();
|
|
dialog_title(ui, "Sign in");
|
|
dialog_text(ui, &if request.for_proxy() { format!("The proxy {host} asks for a user name and password.") } else { format!("{host} asks for a user name and password.") });
|
|
ui.add_space(theme::GAP_L);
|
|
let (mut u, mut p) = ui.data_mut(|d| d.get_temp::<(String, String)>(id)).unwrap_or((username.clone(), password.clone()));
|
|
let width = ui.available_width();
|
|
text_field(ui, &mut u, "User name", width);
|
|
ui.add_space(theme::GAP_S);
|
|
let pr = password_field(ui, &mut p, "Password", width);
|
|
ui.data_mut(|d| d.insert_temp(id, (u.clone(), p.clone())));
|
|
dialog_actions(ui, |ui| {
|
|
if button(ui, ButtonKind::Filled, None, "Sign in").clicked() || (pr.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))) {
|
|
result = Some(DialogResult::Auth { username: u.clone(), password: p.clone() });
|
|
}
|
|
if button(ui, ButtonKind::Text, None, "Cancel").clicked() {
|
|
result = Some(DialogResult::Cancel);
|
|
}
|
|
});
|
|
if ui.input(|i| i.key_pressed(egui::Key::Escape)) {
|
|
result = Some(DialogResult::Cancel);
|
|
}
|
|
});
|
|
if result.is_some() {
|
|
ctx.data_mut(|d| d.remove::<(String, String)>(id));
|
|
}
|
|
},
|
|
Dialog::Select(select) => {
|
|
let rect = device_rect(ctx, f.page_rect.as_ref(), select.position());
|
|
let multiple = select.allow_select_multiple();
|
|
let mut chosen: Vec<usize> = select.selected_options();
|
|
let area = egui::Area::new(Id::new(("select", tab_id))).fixed_pos(rect.left_bottom()).order(egui::Order::Foreground).constrain(true).show(ctx, |ui| {
|
|
surface(&palette, ctx).show(ui, |ui| {
|
|
ui.set_min_width(rect.width().max(200.0));
|
|
ui.spacing_mut().item_spacing.y = 2.0;
|
|
egui::ScrollArea::vertical().max_height(360.0).show(ui, |ui| {
|
|
let mut option = |ui: &mut Ui, o: &servo::SelectElementOption| {
|
|
let selected = chosen.contains(&o.id);
|
|
let r = ui.add_enabled_ui(!o.is_disabled, |ui| menu_item(ui, if selected { Some(icons::CHECK) } else { None }, &o.label, "", selected)).inner;
|
|
if r.clicked() {
|
|
if multiple {
|
|
if selected { chosen.retain(|i| *i != o.id) } else { chosen.push(o.id) }
|
|
} else {
|
|
chosen = vec![o.id];
|
|
result = Some(DialogResult::Select(chosen.clone()));
|
|
}
|
|
}
|
|
};
|
|
for entry in select.options() {
|
|
match entry {
|
|
servo::SelectElementOptionOrOptgroup::Option(o) => option(ui, o),
|
|
servo::SelectElementOptionOrOptgroup::Optgroup { label, options } => {
|
|
ui.add_space(theme::GAP_XS);
|
|
ui.label(RichText::new(label).font(theme::font(theme::SMALL, Weight::Medium)).color(palette.primary));
|
|
for o in options {
|
|
option(ui, o);
|
|
}
|
|
},
|
|
}
|
|
}
|
|
});
|
|
if multiple {
|
|
ui.add_space(theme::GAP_S);
|
|
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
|
if button(ui, ButtonKind::Tonal, None, "Done").clicked() {
|
|
result = Some(DialogResult::Select(chosen.clone()));
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
let outside = ctx.input(|i| i.pointer.any_pressed() && i.pointer.interact_pos().is_some_and(|p| !area.response.rect.contains(p)));
|
|
if result.is_none() && (outside || ctx.input(|i| i.key_pressed(egui::Key::Escape))) {
|
|
result = Some(if multiple { DialogResult::Select(chosen) } else { DialogResult::Close });
|
|
}
|
|
},
|
|
Dialog::Color(picker, initial) => {
|
|
let rect = device_rect(ctx, f.page_rect.as_ref(), picker.position());
|
|
let id = Id::new(("color", tab_id));
|
|
let mut rgb = ctx.data_mut(|d| d.get_temp::<[u8; 3]>(id)).unwrap_or(*initial);
|
|
egui::Area::new(id).fixed_pos(rect.left_bottom()).order(egui::Order::Foreground).constrain(true).show(ctx, |ui| {
|
|
surface(&palette, ctx).inner_margin(egui::Margin::same(theme::GAP_L as i8)).show(ui, |ui| {
|
|
let mut color = egui::Color32::from_rgb(rgb[0], rgb[1], rgb[2]);
|
|
egui::color_picker::color_picker_color32(ui, &mut color, egui::color_picker::Alpha::Opaque);
|
|
rgb = [color.r(), color.g(), color.b()];
|
|
dialog_actions(ui, |ui| {
|
|
if button(ui, ButtonKind::Filled, None, "Choose").clicked() {
|
|
result = Some(DialogResult::Color(Some(rgb)));
|
|
}
|
|
if button(ui, ButtonKind::Text, None, "Cancel").clicked() {
|
|
result = Some(DialogResult::Color(None));
|
|
}
|
|
});
|
|
});
|
|
});
|
|
ctx.data_mut(|d| d.insert_temp(id, rgb));
|
|
if result.is_some() {
|
|
ctx.data_mut(|d| d.remove::<[u8; 3]>(id));
|
|
}
|
|
},
|
|
Dialog::ContextMenu(menu) => {
|
|
super::menus::page_menu(ctx, f, tab_id, menu);
|
|
},
|
|
Dialog::Bluetooth(request) => {
|
|
card(ctx, "bluetooth", 400.0, &mut |ui| {
|
|
dialog_title(ui, "Choose a Bluetooth device");
|
|
if request.devices().is_empty() {
|
|
dialog_text(ui, "No devices found nearby. Check that the device is on and ready to pair.");
|
|
}
|
|
ui.spacing_mut().item_spacing.y = 2.0;
|
|
for (i, device) in request.devices().iter().enumerate() {
|
|
let name = if device.name.is_empty() { device.address.clone() } else { format!("{} ({})", device.name, device.address) };
|
|
if menu_item(ui, Some(icons::BLUETOOTH), &name, "", false).clicked() {
|
|
result = Some(DialogResult::Device(Some(i)));
|
|
}
|
|
}
|
|
dialog_actions(ui, |ui| {
|
|
if button(ui, ButtonKind::Text, None, "Cancel").clicked() || ui.input(|i| i.key_pressed(egui::Key::Escape)) {
|
|
result = Some(DialogResult::Device(None));
|
|
}
|
|
});
|
|
});
|
|
},
|
|
Dialog::LeavePage(_) => {
|
|
card(ctx, "leave", 400.0, &mut |ui| {
|
|
dialog_title(ui, "Leave this page?");
|
|
dialog_text(ui, "Changes you made may not be saved.");
|
|
dialog_actions(ui, |ui| {
|
|
if button(ui, ButtonKind::Filled, None, "Stay").clicked() || ui.input(|i| i.key_pressed(egui::Key::Escape)) {
|
|
result = Some(DialogResult::Leave(false));
|
|
}
|
|
if button(ui, ButtonKind::Text, None, "Leave").clicked() {
|
|
result = Some(DialogResult::Leave(true));
|
|
}
|
|
});
|
|
});
|
|
},
|
|
Dialog::Logins { x, y, items } => {
|
|
let zoom = f.tabs.get(active).map(|t| t.zoom).unwrap_or(1.0);
|
|
let pos = page.min + vec2(x * zoom, y * zoom + 2.0);
|
|
let locked = f.shared.vault.borrow().locked();
|
|
let mut close = false;
|
|
let area = egui::Area::new(Id::new(("logins", tab_id))).fixed_pos(pos).order(egui::Order::Foreground).constrain(true).show(ctx, |ui| {
|
|
surface(&palette, ctx).show(ui, |ui| {
|
|
ui.set_min_width(260.0);
|
|
ui.spacing_mut().item_spacing.y = 2.0;
|
|
if locked {
|
|
let id = Id::new("vault-unlock");
|
|
let mut pw = ui.data_mut(|d| d.get_temp::<String>(id)).unwrap_or_default();
|
|
ui.add_space(theme::GAP_XS);
|
|
ui.label(RichText::new("Unlock saved passwords").font(theme::font(theme::BODY, Weight::Medium)).color(palette.on_surface));
|
|
ui.add_space(theme::GAP_S);
|
|
let r = password_field(ui, &mut pw, "Master password", 260.0);
|
|
if r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
|
|
match f.shared.vault.borrow_mut().unlock(&pw) {
|
|
Ok(()) => {
|
|
pw.clear();
|
|
close = true;
|
|
},
|
|
Err(_) => {
|
|
ui.data_mut(|d| d.insert_temp(id.with("error"), true));
|
|
},
|
|
}
|
|
}
|
|
if ui.data(|d| d.get_temp::<bool>(id.with("error"))).unwrap_or(false) {
|
|
ui.label(RichText::new("That master password is not right.").font(theme::font(theme::SMALL, Weight::Regular)).color(palette.error));
|
|
}
|
|
ui.data_mut(|d| d.insert_temp(id, pw));
|
|
return;
|
|
}
|
|
for (id, user) in items {
|
|
let label = if user.is_empty() { "No user name".to_owned() } else { user.clone() };
|
|
if menu_item(ui, Some(icons::KEY), &label, "", false).clicked() {
|
|
extra.push(Command::FillLogin { tab: tab_id, id: *id });
|
|
}
|
|
}
|
|
let (len, symbols) = {
|
|
let s = f.shared.settings.borrow();
|
|
(s.vault.gen_length, s.vault.gen_symbols)
|
|
};
|
|
if menu_item(ui, Some(icons::PASSWORD), "Use a strong password", "", false).on_hover_text("Fills a new random password; it is saved when you submit the form").clicked() {
|
|
extra.push(Command::FillPassword { tab: tab_id, password: crate::store::vault::generate(len as usize, symbols) });
|
|
}
|
|
if menu_item(ui, Some(icons::SETTINGS), "Manage passwords", "", false).clicked() {
|
|
extra.push(Command::ShowPage(crate::tabs::Page::Passwords));
|
|
close = true;
|
|
}
|
|
});
|
|
});
|
|
let outside = ctx.input(|i| i.pointer.any_pressed() && i.pointer.interact_pos().is_some_and(|p| !area.response.rect.contains(p)));
|
|
if close || outside || ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
|
|
result = Some(DialogResult::Close);
|
|
}
|
|
},
|
|
Dialog::Forms { kind, x, y } => {
|
|
let zoom = f.tabs.get(active).map(|t| t.zoom).unwrap_or(1.0);
|
|
let pos = page.min + vec2(x * zoom, y * zoom + 2.0);
|
|
let area = egui::Area::new(Id::new(("forms", tab_id))).fixed_pos(pos).order(egui::Order::Foreground).constrain(true).show(ctx, |ui| {
|
|
surface(&palette, ctx).show(ui, |ui| {
|
|
ui.set_min_width(260.0);
|
|
ui.spacing_mut().item_spacing.y = 2.0;
|
|
let vault = f.shared.vault.borrow();
|
|
if kind == "card" {
|
|
for c in &vault.contents.cards {
|
|
let label = if c.nickname.is_empty() { c.masked() } else { format!("{} {}", c.nickname, c.masked()) };
|
|
if menu_item(ui, Some(icons::CREDIT_CARD), &label, "", false).clicked() {
|
|
extra.push(Command::FillCard { tab: tab_id, id: c.id });
|
|
}
|
|
}
|
|
} else {
|
|
for a in &vault.contents.addresses {
|
|
if menu_item(ui, Some(icons::PIN_DROP), &format!("{}, {}", a.name, a.street), "", false).clicked() {
|
|
extra.push(Command::FillAddress { tab: tab_id, id: a.id });
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
let outside = ctx.input(|i| i.pointer.any_pressed() && i.pointer.interact_pos().is_some_and(|p| !area.response.rect.contains(p)));
|
|
if outside || ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
|
|
result = Some(DialogResult::Close);
|
|
}
|
|
},
|
|
}
|
|
// Put it back; the app takes it when the result arrives.
|
|
if let Some(tab) = f.tabs.get_mut(active) {
|
|
if tab.dialog.is_none() {
|
|
tab.dialog = Some(dialog);
|
|
}
|
|
}
|
|
for command in extra {
|
|
f.send(command);
|
|
}
|
|
if let Some(result) = result {
|
|
ctx.data_mut(|d| d.insert_temp(Id::new("dialog-count"), count + 1));
|
|
f.send(Command::Dialog { tab: tab_id, result });
|
|
}
|
|
let _ = pos2(0.0, 0.0);
|
|
}
|
|
|
|
fn device_rect(ctx: &egui::Context, page: Option<&egui::Rect>, rect: servo::DeviceIntRect) -> egui::Rect {
|
|
let ppp = ctx.pixels_per_point();
|
|
let origin = page.map(|r| r.min).unwrap_or_default();
|
|
egui::Rect::from_min_max(origin + egui::vec2(rect.min.x as f32 / ppp, rect.min.y as f32 / ppp), origin + egui::vec2(rect.max.x as f32 / ppp, rect.max.y as f32 / ppp))
|
|
}
|