fix: suppress keybinds in input mode, fix profiles, add slide-in errors
This commit is contained in:
1 parent
59ad22e6e8
commit
dcd62f06d7
8 files changed
+301
-87
No files matched your search
@@ -8,7 +8,7 @@ use ratatui::{
|
||||
use tracing::Level;
|
||||
|
||||
use super::base::PopupFrame;
|
||||
use crate::tui::error_buffer::{ErrorEvent, AUTO_DISMISS_MS, SLIDE_START_MS};
|
||||
use crate::tui::error_buffer::{ErrorEvent, AUTO_DISMISS_MS};
|
||||
use crate::tui::theme::THEME;
|
||||
|
||||
pub struct ErrorPopup {
|
||||
@@ -74,29 +74,10 @@ pub fn popup_area(frame_area: Rect, message: &str, base_y: u16, elapsed_ms: u128
|
||||
let popup_w = popup_w.min(frame_area.width.saturating_sub(4));
|
||||
let popup_h = popup_h.min(frame_area.height.saturating_sub(2));
|
||||
let base_x = frame_area.width.saturating_sub(popup_w + 2);
|
||||
|
||||
if elapsed_ms < SLIDE_START_MS {
|
||||
return Some(Rect {
|
||||
x: base_x,
|
||||
y: base_y,
|
||||
width: popup_w,
|
||||
height: popup_h,
|
||||
});
|
||||
}
|
||||
|
||||
let progress = (elapsed_ms - SLIDE_START_MS) as f32 / (AUTO_DISMISS_MS - SLIDE_START_MS) as f32;
|
||||
let slide = (progress * popup_w as f32) as u16;
|
||||
let new_x = base_x.saturating_add(slide);
|
||||
|
||||
if new_x >= frame_area.width {
|
||||
return None;
|
||||
}
|
||||
|
||||
let visible_w = frame_area.width.saturating_sub(new_x);
|
||||
Some(Rect {
|
||||
x: new_x,
|
||||
x: base_x,
|
||||
y: base_y,
|
||||
width: visible_w.min(popup_w),
|
||||
width: popup_w,
|
||||
height: popup_h,
|
||||
})
|
||||
}
|
||||
@@ -5,7 +5,6 @@ use crate::tui::theme::THEME;
|
||||
use crate::tui::widgets::profiles;
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use once_cell::sync::Lazy;
|
||||
use ratatui_textarea::{Input, TextArea};
|
||||
use ratatui::{
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
style::{Modifier, Style},
|
||||
@@ -14,6 +13,7 @@ use ratatui::{
|
||||
Frame,
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tui_prompts::{FocusState, State as PromptState, TextState};
|
||||
|
||||
static WIZARD_STATE: Lazy<Arc<Mutex<WizardState>>> = Lazy::new(|| Arc::new(Mutex::new(WizardState::default())));
|
||||
static WIZARD_RESULT: Lazy<Arc<Mutex<Option<WizardParams>>>> = Lazy::new(|| Arc::new(Mutex::new(None)));
|
||||
@@ -53,7 +53,7 @@ impl<T> Default for LoadState<T> {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WizardState {
|
||||
pub step: WizardStep,
|
||||
pub name_textarea: TextArea<'static>,
|
||||
pub name_state: TextState<'static>,
|
||||
pub versions: LoadState<Vec<GameVersion>>,
|
||||
pub version_idx: usize,
|
||||
pub show_snapshots: bool,
|
||||
@@ -65,13 +65,9 @@ pub struct WizardState {
|
||||
|
||||
impl Default for WizardState {
|
||||
fn default() -> Self {
|
||||
let mut textarea = TextArea::default();
|
||||
textarea.set_cursor_line_style(Style::default());
|
||||
textarea.set_placeholder_text("Instance name...");
|
||||
|
||||
Self {
|
||||
step: WizardStep::Name,
|
||||
name_textarea: textarea,
|
||||
name_state: TextState::new().with_focus(FocusState::Focused),
|
||||
versions: LoadState::Idle,
|
||||
version_idx: 0,
|
||||
show_snapshots: false,
|
||||
@@ -240,23 +236,17 @@ fn handle_name_key(
|
||||
profiles_state: &mut profiles::State,
|
||||
) {
|
||||
match key_event.code {
|
||||
KeyCode::Esc | KeyCode::Left | KeyCode::Char('h') => {
|
||||
KeyCode::Esc => {
|
||||
close_popup(state, profiles_state);
|
||||
}
|
||||
KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') => {
|
||||
if state
|
||||
.name_textarea
|
||||
.lines()
|
||||
.first()
|
||||
.map(|line| line.trim().is_empty())
|
||||
.unwrap_or(true)
|
||||
{
|
||||
KeyCode::Enter => {
|
||||
if state.name_state.value().trim().is_empty() {
|
||||
return;
|
||||
}
|
||||
state.step = WizardStep::Loader;
|
||||
}
|
||||
_ => {
|
||||
state.name_textarea.input(Input::from(key_event.clone()));
|
||||
state.name_state.handle_key_event(key_event.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -427,12 +417,7 @@ fn handle_confirm_key(
|
||||
};
|
||||
|
||||
let params = WizardParams {
|
||||
name: state
|
||||
.name_textarea
|
||||
.lines()
|
||||
.first()
|
||||
.map(|line| line.trim().to_string())
|
||||
.unwrap_or_default(),
|
||||
name: state.name_state.value().trim().to_string(),
|
||||
game_version: selected_version,
|
||||
loader: state.selected_loader(),
|
||||
loader_version: if state.selected_loader() == ModLoader::Vanilla {
|
||||
@@ -483,9 +468,16 @@ fn step_keybinds(state: &WizardState) -> ratatui::text::Line<'static> {
|
||||
}
|
||||
|
||||
fn render_name_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffer::Buffer) {
|
||||
let mut textarea = state.name_textarea.clone();
|
||||
textarea.set_style(Style::default().fg(THEME.colors.foreground));
|
||||
textarea.render(area, buf);
|
||||
let value = state.name_state.value();
|
||||
let display = if value.is_empty() {
|
||||
"█".to_string()
|
||||
} else {
|
||||
format!("{}█", value)
|
||||
};
|
||||
|
||||
Paragraph::new(display)
|
||||
.style(Style::default().fg(THEME.colors.foreground))
|
||||
.render(area, buf);
|
||||
}
|
||||
|
||||
fn render_version_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffer::Buffer) {
|
||||
@@ -617,13 +609,7 @@ fn render_confirm_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffe
|
||||
Paragraph::new(vec![
|
||||
Line::from(vec![
|
||||
Span::styled("Name: ", label_style),
|
||||
Span::raw(
|
||||
state.name_textarea
|
||||
.lines()
|
||||
.first()
|
||||
.cloned()
|
||||
.unwrap_or_default(),
|
||||
),
|
||||
Span::raw(state.name_state.value()),
|
||||
]),
|
||||
Line::from(vec![
|
||||
Span::styled("MC: ", label_style),
|
||||
|
||||
+44
-26
@@ -119,27 +119,34 @@ impl State {
|
||||
|
||||
impl WidgetKey for State {
|
||||
fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) {
|
||||
if self.search.active {
|
||||
match key_event.code {
|
||||
KeyCode::Esc => {
|
||||
self.search.deactivate();
|
||||
self.list_state.selected = Some(0);
|
||||
self.update_scrollbar();
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
self.search.pop();
|
||||
self.list_state.selected = Some(0);
|
||||
self.update_scrollbar();
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
self.search.push(c);
|
||||
self.list_state.selected = Some(0);
|
||||
self.update_scrollbar();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
match key_event.code {
|
||||
KeyCode::Char('/') if !self.search.active => {
|
||||
KeyCode::Char('/') => {
|
||||
self.search.activate();
|
||||
self.list_state.selected = Some(0);
|
||||
self.update_scrollbar();
|
||||
}
|
||||
KeyCode::Esc if self.search.active => {
|
||||
self.search.deactivate();
|
||||
self.list_state.selected = Some(0);
|
||||
self.update_scrollbar();
|
||||
}
|
||||
KeyCode::Backspace if self.search.active => {
|
||||
self.search.pop();
|
||||
self.list_state.selected = Some(0);
|
||||
self.update_scrollbar();
|
||||
}
|
||||
KeyCode::Char(c) if self.search.active && c != 'j' && c != 'k' => {
|
||||
self.search.push(c);
|
||||
self.list_state.selected = Some(0);
|
||||
self.update_scrollbar();
|
||||
}
|
||||
KeyCode::Char('a') => {
|
||||
self.show_popup = true;
|
||||
self.update_scrollbar();
|
||||
@@ -183,31 +190,42 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
|
||||
let idx = filtered[context.index];
|
||||
let instance = &state.instances[idx];
|
||||
|
||||
let name_line = Line::from(vec![
|
||||
Span::raw(" "),
|
||||
Span::styled(
|
||||
instance.name.as_str(),
|
||||
let (name_style, info_style, bg) = if context.is_selected {
|
||||
(
|
||||
Style::default()
|
||||
.fg(THEME.colors.row_highlight)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
Style::default().fg(THEME.colors.foreground),
|
||||
THEME.colors.row_alternate_bg,
|
||||
)
|
||||
} else {
|
||||
(
|
||||
Style::default()
|
||||
.fg(THEME.colors.border_focused)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Style::default().fg(THEME.colors.border_unfocused),
|
||||
THEME.colors.row_background,
|
||||
)
|
||||
};
|
||||
|
||||
let name_line = Line::from(vec![
|
||||
Span::raw(" "),
|
||||
Span::styled(instance.name.as_str(), name_style),
|
||||
]);
|
||||
let info_line = Line::from(vec![
|
||||
Span::raw(" "),
|
||||
Span::styled(
|
||||
format!("{} \u{00b7} {}", instance.game_version, instance.loader),
|
||||
Style::default().fg(THEME.colors.border_unfocused),
|
||||
info_style,
|
||||
),
|
||||
]);
|
||||
let spacer = Line::from("");
|
||||
|
||||
let item = Text::from(vec![name_line, info_line, spacer]);
|
||||
let item = Text::from(vec![name_line, info_line, spacer]).style(Style::default().bg(bg));
|
||||
(item, 3)
|
||||
});
|
||||
|
||||
let list = ListView::new(builder, count)
|
||||
.block(block)
|
||||
.style(Style::default().bg(THEME.colors.row_alternate_bg));
|
||||
let list = ListView::new(builder, count).block(block);
|
||||
|
||||
frame.render_stateful_widget(list, area, &mut state.list_state);
|
||||
|
||||
|
||||
Reference in new issue
Block a user