tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

shell.rs (2707B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 pub fn ui(app: &mut super::Gui, ui: &mut egui::Ui) {
      6    let area = ui.max_rect();
      7    let mut log_area = area;
      8    let mut cmd_area = area;
      9    log_area.max.y -= 20.0;
     10    cmd_area.min.y = log_area.max.y;
     11 
     12    ui.scope_builder(egui::UiBuilder::new().max_rect(log_area), |ui| {
     13        egui::ScrollArea::vertical()
     14            .auto_shrink([false; 2])
     15            .stick_to_bottom(true)
     16            .show(ui, |ui| {
     17                ui.spacing_mut().item_spacing.y = 2.0;
     18                for line in &app.data_model.log {
     19                    ui.label(egui::RichText::new(line).monospace());
     20                }
     21            });
     22    });
     23 
     24    ui.scope_builder(egui::UiBuilder::new().max_rect(cmd_area), |ui| {
     25        // Command input
     26        let _response = ui.horizontal(|ui| {
     27            ui.label(">");
     28            let size = ui.available_size();
     29            let text_edit = egui::TextEdit::singleline(&mut app.data_model.cmd)
     30                .hint_text("Enter command (or help)")
     31                .desired_width(size.x - 20.0);
     32            let response = ui.add(text_edit);
     33 
     34            if response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
     35                let cmd = std::mem::take(&mut app.data_model.cmd);
     36 
     37                let push_history = match app.cmd_history.last() {
     38                    Some(prev) => *prev != cmd,
     39                    None => true,
     40                };
     41                if push_history {
     42                    app.cmd_history.push(cmd.clone());
     43                }
     44                app.cmd_history_index = app.cmd_history.len();
     45 
     46                app.handle_command(&cmd);
     47                response.request_focus();
     48            }
     49 
     50            // Handle history navigation
     51            if response.has_focus() {
     52                if ui.input(|i| i.key_pressed(egui::Key::ArrowUp)) && !app.cmd_history.is_empty() {
     53                    if app.cmd_history_index > 0 {
     54                        app.cmd_history_index -= 1;
     55                        app.data_model.cmd = app.cmd_history[app.cmd_history_index].clone();
     56                    }
     57                }
     58                if ui.input(|i| i.key_pressed(egui::Key::ArrowDown)) && !app.cmd_history.is_empty() {
     59                    if app.cmd_history_index < app.cmd_history.len() - 1 {
     60                        app.cmd_history_index += 1;
     61                        app.data_model.cmd = app.cmd_history[app.cmd_history_index].clone();
     62                    }
     63                }
     64            }
     65 
     66            response
     67        });
     68    });
     69 }