293 lines
10 KiB
Rust
293 lines
10 KiB
Rust
//! UI panels. They only read [`AppState`] and push [`Action`]s.
|
||
|
||
pub mod request;
|
||
pub mod response;
|
||
pub mod sidebar;
|
||
pub mod widgets;
|
||
pub mod windows;
|
||
|
||
use egui::{Context, Key, KeyboardShortcut, Modifiers, RichText, Ui};
|
||
|
||
use crate::action::Action;
|
||
use crate::state::{AppState, QuickOpen};
|
||
use widgets::VarCtx;
|
||
|
||
pub fn draw(ui: &mut Ui, state: &AppState, acts: &mut Vec<Action>) {
|
||
shortcuts(ui.ctx(), state, acts);
|
||
|
||
egui::Panel::top("menu").show(ui, |ui| menu_bar(ui, state, acts));
|
||
egui::Panel::bottom("status").show(ui, |ui| status_bar(ui, state, acts));
|
||
egui::Panel::left("sidebar")
|
||
.resizable(true)
|
||
.default_size(250.0)
|
||
.show(ui, |ui| sidebar::show(ui, state, acts));
|
||
egui::CentralPanel::default().show(ui, |ui| {
|
||
tab_bar(ui, state, acts);
|
||
match state.active_tab() {
|
||
Some(tab) => {
|
||
let resolver = state.resolver_for(tab);
|
||
let is_private = |n: &str| state.is_private_var(n);
|
||
let vars = VarCtx {
|
||
resolver: &resolver,
|
||
is_private: &is_private,
|
||
};
|
||
if tab.orphaned {
|
||
ui.label(
|
||
RichText::new("This request is no longer in its file. Save writes it to a file you pick.")
|
||
.color(ui.visuals().warn_fg_color),
|
||
);
|
||
}
|
||
request::url_bar(ui, state, tab, &vars, acts);
|
||
ui.add_space(4.0);
|
||
egui::Panel::top(("request-editor", tab.id))
|
||
.resizable(true)
|
||
.default_size(250.0)
|
||
.size_range(80.0..=f32::INFINITY)
|
||
.show(ui, |ui| request::editor(ui, state, tab, &vars, acts));
|
||
egui::CentralPanel::no_frame().show(ui, |ui| {
|
||
ui.add_space(4.0);
|
||
response::show(ui, tab, acts);
|
||
});
|
||
}
|
||
None => welcome(ui, acts),
|
||
}
|
||
});
|
||
|
||
windows::show(ui.ctx(), state, acts);
|
||
}
|
||
|
||
fn shortcuts(ctx: &Context, state: &AppState, acts: &mut Vec<Action>) {
|
||
if state.dialog.is_some() {
|
||
return;
|
||
}
|
||
let cmd = |k| KeyboardShortcut::new(Modifiers::COMMAND, k);
|
||
let active = state.active;
|
||
ctx.input_mut(|i| {
|
||
if i.consume_shortcut(&cmd(Key::Enter)) {
|
||
if let Some(id) = active {
|
||
acts.push(Action::Send(id));
|
||
}
|
||
}
|
||
if i.consume_shortcut(&cmd(Key::S)) {
|
||
if let Some(id) = active {
|
||
acts.push(Action::Save(id));
|
||
}
|
||
}
|
||
if i.consume_shortcut(&cmd(Key::T)) {
|
||
acts.push(Action::NewTab);
|
||
}
|
||
if i.consume_shortcut(&cmd(Key::W)) {
|
||
if let Some(id) = active {
|
||
acts.push(Action::CloseTab(id));
|
||
}
|
||
}
|
||
if i.consume_shortcut(&cmd(Key::L)) {
|
||
acts.push(Action::FocusUrl);
|
||
}
|
||
if i.consume_shortcut(&cmd(Key::P)) {
|
||
acts.push(Action::QuickOpen(Some(QuickOpen::default())));
|
||
}
|
||
if i.consume_shortcut(&cmd(Key::F)) {
|
||
if let Some(id) = active {
|
||
acts.push(Action::SearchOpen(id, true));
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
fn menu_bar(ui: &mut Ui, state: &AppState, acts: &mut Vec<Action>) {
|
||
egui::MenuBar::new().ui(ui, |ui| {
|
||
ui.menu_button("File", |ui| {
|
||
if ui.button("New tab Ctrl+T").clicked() {
|
||
acts.push(Action::NewTab);
|
||
ui.close();
|
||
}
|
||
if ui.button("Open collection folder…").clicked() {
|
||
acts.push(Action::PickCollection);
|
||
ui.close();
|
||
}
|
||
ui.menu_button("Recent collections", |ui| {
|
||
for p in &state.settings.recent_collections {
|
||
if ui.button(p.display().to_string()).clicked() {
|
||
acts.push(Action::OpenCollection(p.clone()));
|
||
ui.close();
|
||
}
|
||
}
|
||
if state.settings.recent_collections.is_empty() {
|
||
ui.label(RichText::new("None yet").weak());
|
||
}
|
||
});
|
||
ui.separator();
|
||
let has_tab = state.active.is_some();
|
||
if ui
|
||
.add_enabled(has_tab, egui::Button::new("Save Ctrl+S"))
|
||
.clicked()
|
||
{
|
||
acts.push(Action::Save(state.active.unwrap()));
|
||
ui.close();
|
||
}
|
||
ui.separator();
|
||
if ui.button("Import Postman collection…").clicked() {
|
||
acts.push(Action::ImportPostman);
|
||
ui.close();
|
||
}
|
||
ui.label(
|
||
RichText::new("Tip: paste a curl command into the URL bar to import it.")
|
||
.small()
|
||
.weak(),
|
||
);
|
||
ui.separator();
|
||
if ui.button("Settings…").clicked() {
|
||
acts.push(Action::SetWindows(crate::state::Windows {
|
||
settings: true,
|
||
..state.windows
|
||
}));
|
||
ui.close();
|
||
}
|
||
if ui.button("Quit").clicked() {
|
||
acts.push(Action::CloseRequested);
|
||
ui.close();
|
||
}
|
||
});
|
||
ui.menu_button("View", |ui| {
|
||
let mut w = state.windows;
|
||
ui.checkbox(&mut w.history, "History");
|
||
ui.checkbox(&mut w.cookies, "Cookies");
|
||
if w != state.windows {
|
||
acts.push(Action::SetWindows(w));
|
||
}
|
||
if ui.button("Quick open… Ctrl+P").clicked() {
|
||
acts.push(Action::QuickOpen(Some(QuickOpen::default())));
|
||
ui.close();
|
||
}
|
||
});
|
||
ui.menu_button("Help", |ui| {
|
||
if ui.button("About and shortcuts").clicked() {
|
||
acts.push(Action::SetWindows(crate::state::Windows {
|
||
about: true,
|
||
..state.windows
|
||
}));
|
||
ui.close();
|
||
}
|
||
});
|
||
|
||
// Environment picker on the right.
|
||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||
if let Some(c) = &state.collection {
|
||
if ui.button("Edit…").on_hover_text("Edit environments").clicked() {
|
||
acts.push(Action::OpenEnvEditor);
|
||
}
|
||
let names = c.envs.names();
|
||
let label = state.selected_env.clone().unwrap_or_else(|| "No environment".into());
|
||
egui::ComboBox::from_id_salt("env-picker")
|
||
.selected_text(label)
|
||
.width(150.0)
|
||
.show_ui(ui, |ui| {
|
||
if ui
|
||
.selectable_label(state.selected_env.is_none(), "No environment")
|
||
.clicked()
|
||
{
|
||
acts.push(Action::SelectEnv(None));
|
||
}
|
||
for n in names {
|
||
if ui
|
||
.selectable_label(state.selected_env.as_ref() == Some(&n), &n)
|
||
.clicked()
|
||
{
|
||
acts.push(Action::SelectEnv(Some(n.clone())));
|
||
}
|
||
}
|
||
});
|
||
ui.label("Environment:");
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
fn tab_bar(ui: &mut Ui, state: &AppState, acts: &mut Vec<Action>) {
|
||
egui::ScrollArea::horizontal().id_salt("tabs").show(ui, |ui| {
|
||
ui.horizontal(|ui| {
|
||
for t in &state.tabs {
|
||
let selected = state.active == Some(t.id);
|
||
let mut title = t.title();
|
||
if t.is_dirty() {
|
||
title.push_str(" •");
|
||
}
|
||
if t.in_flight.is_some() {
|
||
title.push_str(" ⏳");
|
||
}
|
||
let resp = ui.selectable_label(selected, title);
|
||
let resp = match &t.origin {
|
||
Some(o) => resp.on_hover_text(o.file.display().to_string()),
|
||
None => resp,
|
||
};
|
||
if resp.clicked() {
|
||
acts.push(Action::SelectTab(t.id));
|
||
}
|
||
if resp.middle_clicked() {
|
||
acts.push(Action::CloseTab(t.id));
|
||
}
|
||
resp.context_menu(|ui| {
|
||
if ui.button("Close").clicked() {
|
||
acts.push(Action::CloseTab(t.id));
|
||
ui.close();
|
||
}
|
||
});
|
||
if ui.small_button("×").on_hover_text("Close (Ctrl+W)").clicked() {
|
||
acts.push(Action::CloseTab(t.id));
|
||
}
|
||
ui.separator();
|
||
}
|
||
if ui.button("+").on_hover_text("New tab (Ctrl+T)").clicked() {
|
||
acts.push(Action::NewTab);
|
||
}
|
||
});
|
||
});
|
||
ui.separator();
|
||
}
|
||
|
||
fn status_bar(ui: &mut Ui, state: &AppState, acts: &mut Vec<Action>) {
|
||
ui.horizontal(|ui| {
|
||
match &state.status {
|
||
Some(s) => {
|
||
let color = if s.is_error {
|
||
ui.visuals().error_fg_color
|
||
} else {
|
||
ui.visuals().text_color()
|
||
};
|
||
ui.colored_label(color, &s.text);
|
||
if ui.small_button("×").clicked() {
|
||
acts.push(Action::DismissStatus);
|
||
}
|
||
}
|
||
None => {
|
||
ui.label(RichText::new("Ready").weak());
|
||
}
|
||
}
|
||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||
let n = state.tabs.iter().filter(|t| t.in_flight.is_some()).count();
|
||
if n > 0 {
|
||
ui.spinner();
|
||
ui.label(format!("{n} in flight"));
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
fn welcome(ui: &mut Ui, acts: &mut Vec<Action>) {
|
||
ui.vertical_centered(|ui| {
|
||
ui.add_space(60.0);
|
||
ui.heading("reqlite");
|
||
ui.label("Send HTTP requests and keep them as .http files.");
|
||
ui.add_space(12.0);
|
||
if ui.button("New request (Ctrl+T)").clicked() {
|
||
acts.push(Action::NewTab);
|
||
}
|
||
if ui.button("Open a collection folder…").clicked() {
|
||
acts.push(Action::PickCollection);
|
||
}
|
||
ui.add_space(12.0);
|
||
ui.label(RichText::new("Tip: paste a curl command into the URL bar to import it.").weak());
|
||
});
|
||
}
|