Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ impl Default for EditCommandsPopup {
}
}

pub struct ErrorPopup {
pub struct UiPopup {
pub message: String,
pub popup_title: String,
pub visible: bool,
}

impl ErrorPopup {
pub fn new() -> Self {
ErrorPopup {
impl UiPopup {
pub fn new(popup_tile: String) -> Self {
UiPopup {
message: String::new(),
visible: false,
popup_title: popup_tile,
}
}

Expand All @@ -46,9 +48,9 @@ impl ErrorPopup {
}
}

impl Default for ErrorPopup {
impl Default for UiPopup {
fn default() -> Self {
Self::new()
Self::new("".to_owned())
}
}

Expand Down Expand Up @@ -226,7 +228,7 @@ pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup) {
f.render_widget(paragraph, area);
}

pub fn render_error_popup(f: &mut Frame, popup: &ErrorPopup) {
pub fn render_ui_popup(f: &mut Frame, popup: &UiPopup) {
if !popup.visible {
return;
}
Expand All @@ -240,7 +242,7 @@ pub fn render_error_popup(f: &mut Frame, popup: &ErrorPopup) {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Red))
.title("Error - Esc to exit"),
.title(format!("{} - Esc to exit", popup.popup_title)),
);
f.render_widget(text, area);
}
Expand Down
76 changes: 62 additions & 14 deletions src/ui_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use tui_textarea::TextArea;
use crate::{
format_json, format_markdown, get_save_file_path, load_textareas, save_textareas,
ui::{
render_edit_commands_popup, render_error_popup, render_header, render_title_popup,
render_title_select_popup, EditCommandsPopup, ErrorPopup,
render_edit_commands_popup, render_header, render_title_popup, render_title_select_popup,
render_ui_popup, EditCommandsPopup, UiPopup,
},
ScrollableTextArea, TitlePopup, TitleSelectPopup,
};
Expand All @@ -30,7 +30,8 @@ pub struct UIState {
pub scrollable_textarea: ScrollableTextArea,
pub title_popup: TitlePopup,
pub title_select_popup: TitleSelectPopup,
pub error_popup: ErrorPopup,
pub error_popup: UiPopup,
pub copy_popup: UiPopup,
pub edit_commands_popup: EditCommandsPopup,
pub clipboard: Option<EditorClipboard>,
pub last_draw: Instant,
Expand All @@ -54,7 +55,8 @@ impl UIState {
scrollable_textarea,
title_popup: TitlePopup::new(),
title_select_popup: TitleSelectPopup::new(),
error_popup: ErrorPopup::new(),
error_popup: UiPopup::new("Error".to_string()),
copy_popup: UiPopup::new("Block Copied".to_string()),
edit_commands_popup: EditCommandsPopup::new(),
clipboard: EditorClipboard::try_new(),
last_draw: Instant::now(),
Expand Down Expand Up @@ -96,7 +98,11 @@ pub fn draw_ui(
}

if state.error_popup.visible {
render_error_popup(f, &state.error_popup);
render_ui_popup(f, &state.error_popup);
}

if state.copy_popup.visible {
render_ui_popup(f, &state.copy_popup);
}
})?;
Ok(())
Expand Down Expand Up @@ -138,10 +144,30 @@ fn handle_full_screen_input(state: &mut UIState, key: event::KeyEvent) -> Result
KeyCode::Up => handle_up_key(state, key),
KeyCode::Down => handle_down_key(state, key),
KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL) => {
if let Err(e) = state.scrollable_textarea.copy_focused_textarea_contents() {
state
.error_popup
.show(format!("Failed to copy to clipboard: {}", e));
match state.scrollable_textarea.copy_focused_textarea_contents() {
Ok(_) => {
let curr_focused_index = state.scrollable_textarea.focused_index;
let curr_title_option =
state.scrollable_textarea.titles.get(curr_focused_index);

match curr_title_option {
Some(curr_title) => {
state
.copy_popup
.show(format!("Copied block {}", curr_title));
}
None => {
state
.error_popup
.show("Failed to copy selection with title".to_string());
}
}
}
Err(e) => {
state
.error_popup
.show(format!("Failed to copy to system clipboard: {}", e));
}
}
}
KeyCode::Char('s')
Expand Down Expand Up @@ -260,13 +286,32 @@ fn handle_normal_input(
}
}
}
// edit_with_external_editor(state)?;
}
KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL) => {
if let Err(e) = state.scrollable_textarea.copy_focused_textarea_contents() {
state
.error_popup
.show(format!("Failed to copy to clipboard: {}", e));
match state.scrollable_textarea.copy_focused_textarea_contents() {
Ok(_) => {
let curr_focused_index = state.scrollable_textarea.focused_index;
let curr_title_option =
state.scrollable_textarea.titles.get(curr_focused_index);

match curr_title_option {
Some(curr_title) => {
state
.copy_popup
.show(format!("Copied block {}", curr_title));
}
None => {
state
.error_popup
.show("Failed to copy selection with title".to_string());
}
}
}
Err(e) => {
state
.error_popup
.show(format!("Failed to copy to system clipboard: {}", e));
}
}
}
KeyCode::Char('b') if key.modifiers.contains(KeyModifiers::CONTROL) => {
Expand Down Expand Up @@ -361,6 +406,9 @@ fn handle_normal_input(
if state.error_popup.visible {
state.error_popup.hide();
}
if state.copy_popup.visible {
state.copy_popup.hide();
}
}
KeyCode::Up => handle_up_key(state, key),
KeyCode::Down => handle_down_key(state, key),
Expand Down
Loading