Skip to content

Commit

Permalink
made text and lists more clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed Nov 2, 2022
1 parent ebc0749 commit 030eeb9
Show file tree
Hide file tree
Showing 18 changed files with 506 additions and 138 deletions.
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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "speki"
version = "0.4.2"
version = "0.4.3"
edition = "2021"
authors = ["Tor Berge [email protected]"]
license = "GPL-2.0-only"
Expand Down
40 changes: 34 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl TabsState {
fn keyhandler(&mut self, appdata: &AppData, key: MyKey) {
match key {
MyKey::Nav(dir) => self.tabs[self.index].navigate(dir),
key => self.tabs[self.index].keyhandler(appdata, key),
key => self.tabs[self.index].main_keyhandler(appdata, key),
}
}
fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, area: Rect) {
Expand Down Expand Up @@ -261,7 +261,7 @@ pub trait PopUp: Tab {
area.height -= 4;
area.width -= 4;
}
self.render(f, appdata, area);
self.main_render(f, appdata, area);
}
}

Expand All @@ -278,16 +278,41 @@ pub trait Widget {

pub trait Tab {
fn get_title(&self) -> String;
fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, area: Rect);
fn keyhandler(&mut self, appdata: &AppData, key: MyKey);
fn get_manual(&self) -> String {
String::new()
}
fn set_selection(&mut self, area: Rect);
fn get_cursor(&self) -> (u16, u16);
fn navigate(&mut self, dir: NavDir);

fn get_cursor(&mut self) -> (u16, u16) {
self.get_view().clone().cursor
}
fn navigate(&mut self, dir: NavDir) {
if let Some(popup) = self.get_popup() {
popup.navigate(dir);
} else {
self.get_view().navigate(dir);
}
}

fn get_view(&mut self) -> &mut View;

fn main_keyhandler(&mut self, appdata: &AppData, key: MyKey) {
if let Some(popup) = self.get_popup() {
popup.main_keyhandler(appdata, key);
return;
}
if let MyKey::KeyPress(pos) = key.clone() {
self.get_view().cursor = pos;
}
match key {
MyKey::Nav(dir) => self.navigate(dir),
key => self.keyhandler(appdata, key),
}
}
fn keyhandler(&mut self, appdata: &AppData, key: MyKey);

fn main_render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, area: Rect) {
self.set_selection(area);
self.render(f, appdata, area);
if let Some(popup) = self.get_popup() {
if popup.should_quit() {
Expand All @@ -297,6 +322,9 @@ pub trait Tab {
popup.render_popup(f, appdata, area);
}
}

fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, area: Rect);

fn get_popup(&mut self) -> Option<&mut Box<dyn PopUp>> {
None
}
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub enum MyKey {
KeyPress((u16, u16)),
ScrollUp,
ScrollDown,
ScrollLeft,
ScrollRight,
}

#[derive(Clone, PartialEq, Debug)]
Expand All @@ -134,6 +136,12 @@ impl MyKey {
if let Mouse(mouse) = event {
match mouse.kind {
MouseEventKind::Down(_) => return Some(MyKey::KeyPress((mouse.column, mouse.row))),
MouseEventKind::ScrollUp if mouse.modifiers == event::KeyModifiers::SHIFT => {
return Some(MyKey::ScrollLeft)
}
MouseEventKind::ScrollDown if mouse.modifiers == event::KeyModifiers::SHIFT => {
return Some(MyKey::ScrollRight)
}
MouseEventKind::ScrollUp => return Some(MyKey::ScrollUp),
MouseEventKind::ScrollDown => return Some(MyKey::ScrollDown),
_ => {}
Expand Down
19 changes: 5 additions & 14 deletions src/tabs/add_card/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ Add card as unfinished: Alt+u
.to_string()
}

fn get_view(&mut self) -> &mut View {
&mut self.view
}

fn set_selection(&mut self, area: Rect) {
self.view.areas.clear();
let chunks = split_leftright_by_percent([75, 15], area);
Expand All @@ -118,12 +122,7 @@ Add card as unfinished: Alt+u
use MyKey::*;
let cursor = &self.get_cursor();

if let KeyPress(pos) = key {
self.view.cursor = pos;
}

match key {
Nav(dir) => self.navigate(dir),
Alt('f') => self.submit_card(&appdata.conn, true),
Alt('u') => self.submit_card(&appdata.conn, false),
Alt('g') => {
Expand All @@ -138,20 +137,12 @@ Add card as unfinished: Alt+u
_ => {}
}
}
fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, area: Rect) {
self.set_selection(area);
fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, _area: Rect) {
let cursor = &self.get_cursor();

self.topics.render(f, appdata, cursor);
self.prompt.render(f, appdata, cursor);
self.question.render(f, appdata, cursor);
self.answer.render(f, appdata, cursor);
}

fn navigate(&mut self, dir: crate::NavDir) {
self.view.navigate(dir);
}
fn get_cursor(&self) -> (u16, u16) {
self.view.cursor
}
}
20 changes: 3 additions & 17 deletions src/tabs/browse/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,9 @@ impl Tab for Browse {
fn get_title(&self) -> String {
"Browse".to_string()
}
fn get_cursor(&self) -> (u16, u16) {
self.view.cursor
}

fn navigate(&mut self, dir: NavDir) {
self.view.navigate(dir);
fn get_view(&mut self) -> &mut View {
&mut self.view
}

fn set_selection(&mut self, area: Rect) {
Expand Down Expand Up @@ -410,9 +407,8 @@ impl Tab for Browse {
&mut self,
f: &mut tui::Frame<crate::MyType>,
appdata: &crate::app::AppData,
area: Rect,
_area: Rect,
) {
self.set_selection(area);
let cursor = &self.view.cursor;
self.filters.render(f, appdata, cursor);

Expand Down Expand Up @@ -444,17 +440,7 @@ impl Tab for Browse {
use MyKey::*;
let cursor = &self.get_cursor();

if let Some(popup) = &mut self.popup {
popup.keyhandler(appdata, key);
return;
}

if let Nav(dir) = key {
self.navigate(dir);
return;
}
match key {
KeyPress(pos) => self.view.cursor = pos,
Enter | Char(' ') if self.filtered.is_selected(cursor) => {
if let Some(item) = self.filtered.take_selected_item() {
if !self.selected_ids.contains(&item.id) {
Expand Down
8 changes: 7 additions & 1 deletion src/tabs/import/logic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::app::{AppData, Tab, Widget};
use crate::utils::misc::View;
use crate::widgets::message_box::draw_message;
use crate::widgets::topics::TopicList;
use crate::MyKey;
Expand Down Expand Up @@ -161,6 +162,7 @@ pub struct Importer {
selection: Selection,
menu: Menu,
area: Rect,
view: View,
}

impl Importer {
Expand All @@ -175,6 +177,7 @@ impl Importer {
selection,
menu,
area: Rect::default(),
view: View::default(),
}
}

Expand Down Expand Up @@ -203,7 +206,10 @@ impl Importer {

impl Tab for Importer {
fn set_selection(&mut self, _area: Rect) {}
fn get_cursor(&self) -> (u16, u16) {
fn get_view(&mut self) -> &mut crate::utils::misc::View {
&mut self.view
}
fn get_cursor(&mut self) -> (u16, u16) {
(0, 0)
}
fn navigate(&mut self, dir: NavDir) {
Expand Down
64 changes: 22 additions & 42 deletions src/tabs/incread/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::utils::statelist::StatefulList;
use crate::widgets::message_box::draw_message;
use crate::widgets::topics::TopicList;
use tui::layout::Rect;
use tui::widgets::Clear;
use tui::Frame;

use crate::utils::incread::IncListItem;
Expand Down Expand Up @@ -53,9 +52,13 @@ impl Tab for WikiSelect {
"Wikipedia selection".to_string()
}

fn get_view(&mut self) -> &mut View {
todo!()
}

fn navigate(&mut self, _dir: crate::NavDir) {}

fn get_cursor(&self) -> (u16, u16) {
fn get_cursor(&mut self) -> (u16, u16) {
(0, 0)
}

Expand All @@ -79,16 +82,9 @@ impl Tab for WikiSelect {

fn set_selection(&mut self, _area: Rect) {}

fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, mut area: Rect) {
fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, area: Rect) {
let cursor = &self.get_cursor();
if area.height > 10 && area.width > 10 {
area = crate::utils::misc::centered_rect(80, 70, area);
f.render_widget(Clear, area); //this clears out the background
area.x += 2;
area.y += 2;
area.height -= 4;
area.width -= 4;
}

let chunks = split_updown_by_percent([50, 50], area);
let (mut msg, mut search) = (chunks[0], chunks[1]);
msg.y = search.y - 5;
Expand All @@ -107,7 +103,6 @@ pub struct MainInc {
pub topics: TopicList,
pub popup: Option<Box<dyn PopUp>>,
view: View,
area: Rect,
}

use crate::utils::sql::fetch::load_extracts;
Expand All @@ -132,7 +127,6 @@ impl MainInc {
topics,
popup,
view,
area: Rect::default(),
}
}

Expand Down Expand Up @@ -178,12 +172,16 @@ impl MainInc {
}

impl Tab for MainInc {
fn get_cursor(&self) -> (u16, u16) {
self.view.cursor
fn get_view(&mut self) -> &mut View {
&mut self.view
}

fn navigate(&mut self, dir: crate::NavDir) {
self.view.navigate(dir);
fn get_popup(&mut self) -> Option<&mut Box<dyn PopUp>> {
if let Some(popup) = &mut self.popup {
Some(popup)
} else {
None
}
}

fn set_selection(&mut self, area: Rect) {
Expand Down Expand Up @@ -233,27 +231,15 @@ make cloze (visual mode): Alt+z
.to_string()
}

fn exit_popup(&mut self, appdata: &AppData) {
self.popup = None;
self.reload_inc_list(&appdata.conn);
}

fn keyhandler(&mut self, appdata: &AppData, key: MyKey) {
use crate::MyKey::*;
let cursor = &self.get_cursor();

if let Some(popup) = &mut self.popup {
popup.keyhandler(appdata, key);
if popup.should_quit() {
self.popup = None;
self.reload_inc_list(&appdata.conn);
}
return;
}

if let MyKey::Nav(dir) = key {
self.view.navigate(dir);
return;
} else if let MyKey::Alt('a') = &key {
self.create_source(&appdata.conn, "".to_string());
return;
}

let incfocus = {
if let Some(inc) = &mut self.focused {
inc.source.is_selected(cursor)
Expand All @@ -263,7 +249,7 @@ make cloze (visual mode): Alt+z
};

match key {
KeyPress(pos) if self.popup.is_none() => self.view.cursor = pos,
MyKey::Alt('a') => self.create_source(&appdata.conn, "".to_string()),
Enter if self.extracts.is_selected(cursor) => self.focus_extracts(&appdata.conn),
Enter if self.inclist.is_selected(cursor) => self.focus_list(&appdata.conn),
Alt('w') => {
Expand All @@ -290,20 +276,14 @@ make cloze (visual mode): Alt+z
}
}

fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, area: Rect) {
self.set_selection(area);
fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, _area: Rect) {
let cursor = &self.get_cursor();

if let Some(inc) = &mut self.focused {
inc.source.render(f, appdata, cursor);
}

self.topics.render(f, appdata, cursor);
self.inclist.render(f, appdata, cursor);
self.extracts.render(f, appdata, cursor);

if let Some(popup) = &mut self.popup {
popup.render(f, appdata, area);
}
}
}
Loading

0 comments on commit 030eeb9

Please sign in to comment.