a7n-gui 0.23.0

Desktop interface for the Apeirogon software KVM
// SPDX-License-Identifier: GPL-3.0-or-later

use eframe::egui;

use crate::{App, theme};
use theme::{ButtonKind, ButtonSize};

use super::chord_editor::chord_editor;

impl App {
    pub(super) fn gestures(&mut self, ui: &mut egui::Ui) {
        theme::card(ui, |ui| {
            ui.heading("Trackpad gestures");
            theme::small(ui, "Shortcuts for swipes received from a Mac trackpad.");
            for (index, (name, title)) in [
                ("swipe_up", "Swipe up"),
                ("swipe_down", "Swipe down"),
                ("swipe_left", "Swipe left"),
                ("swipe_right", "Swipe right"),
            ]
            .into_iter()
            .enumerate()
            {
                ui.separator();
                self.gesture_row(ui, index, name, title);
            }
        });
    }

    fn gesture_row(&mut self, ui: &mut egui::Ui, index: usize, name: &str, title: &str) {
        let key = format!("gestures.{name}");
        theme::setting_row(
            ui,
            title,
            "Three or four fingers. Clear to turn off.",
            Some(&key),
            |ui| {
                ui.horizontal(|ui| self.gesture_editor(ui, index, &key));
            },
        );
    }

    fn gesture_editor(&mut self, ui: &mut egui::Ui, index: usize, key: &str) {
        let mut value = self.value(key, "");
        if value.is_empty() && !self.editing_gestures[index] {
            theme::small(ui, "Off");
        }
        if chord_editor(
            ui,
            &mut value,
            &mut self.editing_gestures[index],
            "ctrl+meta+left",
        ) {
            self.edit(key, &value, "");
        }
        if !value.is_empty()
            && theme::button(ui, ButtonKind::Ghost, ButtonSize::Sm, None, "Clear").clicked()
        {
            self.edit(key, "", "");
        }
    }
}