Skip to content

Commit

Permalink
add markdown tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Jul 23, 2024
1 parent 8bc9167 commit 655802b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 63 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ iced = { git = "https://github.com/iced-rs/iced", features = [
"advanced",
"markdown",
"highlighter",
], branch = "master" }
iced_runtime = { git = "https://github.com/iced-rs/iced", branch = "master" }
], rev = "dcdf1307006883f50083c186ca7b8656bfa60873" }
iced_runtime = { git = "https://github.com/iced-rs/iced", rev = "dcdf1307006883f50083c186ca7b8656bfa60873" }
ngnk = { version = "0.2.3", optional = true }


Expand Down
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::widgets::text_input;
use utils::keval;
use utils::{truncate, HistoryMap, REPL};
use views::pane::{view_pane, Pane};
use views::toolbar::toolbar_view;
use views::toolbar::Toolbar;

pub static INPUT_ID: Lazy<text_input::Id> = Lazy::new(text_input::Id::unique);
static SCROLL_ID: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);
Expand Down Expand Up @@ -55,6 +55,7 @@ struct State {
panes: pane_grid::State<Pane>,
panes_created: usize,
focus: Option<pane_grid::Pane>,
glyphbar: Toolbar,
}

impl Default for State {
Expand All @@ -69,6 +70,7 @@ impl Default for State {
panes,
panes_created: 1,
focus: None,
glyphbar: Toolbar::new(),
}
}
}
Expand Down Expand Up @@ -411,15 +413,15 @@ impl Beacon {
tab_at: at,
focus,
panes,
glyphbar,
..
},
) => {
let glyphbar = toolbar_view();
let tabs = tab_view(outs, *at);

let focus = focus;
let total_panes = panes.len();
let pane_grid = PaneGrid::new(&panes, |id, pane, is_maximized| {
let pane_grid = PaneGrid::new(panes, |id, pane, is_maximized| {
let is_focused = *focus == Some(id);
let pane_outs = outs
.0
Expand All @@ -430,11 +432,10 @@ impl Beacon {
id,
total_panes,
pane.is_pinned,
&input_value,
input_value,
pane_outs,
*at,
)
.into()
}))
.style(if is_focused {
style::pane_focused
Expand All @@ -448,7 +449,7 @@ impl Beacon {
.on_click(Message::Clicked)
.on_drag(Message::Dragged)
.on_resize(10, Message::Resized);
container(column![glyphbar, tabs, pane_grid])
container(column![glyphbar.view(), tabs, pane_grid])
.width(Length::Fill)
.height(Length::Fill)
.padding(10)
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pub fn truncate(s: &str, max_chars: usize) -> &str {
pub mod macros {
macro_rules! bqn386 {
($q:expr) => {
text($q)
iced::widget::text($q)
.font(Font::with_name("BQN386 Unicode"))
.size(14)
.line_height(LineHeight::Absolute(12.into()))
.line_height(iced::widget::text::LineHeight::Absolute(12.into()))
};
}
pub(crate) use bqn386;
Expand Down
16 changes: 4 additions & 12 deletions src/views/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use iced::alignment::Alignment;
use iced::widget::{pane_grid, svg};
use iced::{
color,
widget::{
button, column, container, row, scrollable, text, text::LineHeight, Column, Container,
},
widget::{button, column, container, row, scrollable, Column, Container},
Element, Font, Length,
};

Expand Down Expand Up @@ -42,7 +40,7 @@ pub fn view_pane<'a>(
) -> Element<'a, Message> {
let inp = text_input::TextInput::new(
"",
&input_value
input_value
.get(&unsafe { std::mem::transmute::<_, usize>(pane) })
.unwrap_or(&String::new()),
)
Expand Down Expand Up @@ -133,8 +131,7 @@ pub fn view_pane<'a>(
Message::Split(pane_grid::Axis::Horizontal, pane),
),
button("vertical", Message::Split(pane_grid::Axis::Vertical, pane),)
]
.spacing(5);
];
if total_panes > 1 && !is_pinned {
controls = controls.push(button("cross", Message::Close(pane)));
}
Expand All @@ -146,13 +143,8 @@ pub fn view_pane<'a>(
.id(SCROLL_ID.clone()),
inp
]
.width(Length::Fill)
.spacing(10)
.align_x(Alignment::Center);

container(content)
.width(Length::Fill)
.height(Length::Fill)
.padding(5)
.into()
container(content).height(Length::Fill).padding(5).into()
}
13 changes: 11 additions & 2 deletions src/views/tabs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use iced::{
widget::{button, row, text, text::LineHeight, Container},
widget::{button, row, Container},
Element, Font,
};

Expand All @@ -16,7 +16,7 @@ pub fn tab_view<'a>(outs: &HistoryMap, at: usize) -> Element<'a, Message> {
.map(|i| {
Container::new(
button(if *i == at {
bqn386!(format!("{i}"))
bqn386!(format!("[{i}]"))
} else {
bqn386!(format!("{i}"))
})
Expand All @@ -30,6 +30,15 @@ pub fn tab_view<'a>(outs: &HistoryMap, at: usize) -> Element<'a, Message> {
text_color: iced::Color::WHITE,
..Default::default()
},
button::Status::Pressed => button::Style {
background: Some(iced::Background::Color(iced::Color::from_rgb(
12.0 / 255.0,
12.0 / 255.0,
12.0 / 255.0,
))),
text_color: iced::Color::WHITE,
..Default::default()
},
_ => button::Style {
background: Some(iced::Background::Color(iced::Color::from_rgb(
12.0 / 255.0,
Expand Down
75 changes: 50 additions & 25 deletions src/views/toolbar.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use iced::{
widget::{button, container, markdown, text, text::LineHeight, tooltip},
Element, Font,
widget::{
button, markdown,
tooltip,
},
Element, Font, Padding,
};
use once_cell::sync::Lazy;
use std::collections::HashMap;

use crate::{
docs::content::glyph_to_documentation,
styles::{btnstyle, toolbarstyle},
utils::macros::bqn386,
widgets::wrap::Wrap,
Message,
docs::content::glyph_to_documentation, styles::btnstyle, utils::macros::bqn386,
widgets::wrap::Wrap, Message,
};

static GLYPHS: Lazy<[char; 64]> = Lazy::new(|| {
Expand All @@ -21,30 +22,54 @@ static GLYPHS: Lazy<[char; 64]> = Lazy::new(|| {
]
});

pub fn toolbar_view<'a>() -> Element<'a, Message> {
container(
GLYPHS
pub struct Toolbar {
items: HashMap<char, Vec<markdown::Item>>,
}

impl Toolbar {
pub fn new() -> Self {
let items: HashMap<char, Vec<markdown::Item>> = GLYPHS
.iter()
.fold(Wrap::new(), |wrap, glyph| {
wrap.push(
.map(|&glyph| (glyph, parse_glyph_documentation(glyph)))
.collect();

Self { items }
}

pub fn view(&self) -> Element<'_, Message> {
Wrap::with_elements(
GLYPHS
.iter()
.map(|&glyph| {
tooltip(
button(bqn386!(glyph))
.style(btnstyle)
.on_press(Message::ToolbarClick(glyph.to_string())),
markdown(
markdown::parse(
glyph_to_documentation(*glyph),
iced::theme::Palette::DRACULA,
),
iced::widget::container(markdown(
&self.items[&glyph],
markdown::Settings::default(),
(),
),
|url| Message::TabNext,
))
.style(|t| iced::widget::container::Style {
text_color: Some(iced::Color::BLACK),
background: Some(iced::Background::Color(iced::Color::WHITE)),
border: iced::border::rounded(4),
shadow: Default::default(),
})
.padding(Padding::new(4.0))
.width(400.0)
.max_height(1000.0),
tooltip::Position::FollowCursor,
)
.style(toolbarstyle),
)
})
.spacing(1.0),
)
.into()
.into()
})
.collect(),
)
.into()
}
}

fn parse_glyph_documentation(glyph: char) -> Vec<markdown::Item> {
markdown::parse(glyph_to_documentation(glyph), iced::theme::Palette::DRACULA).collect()
}

0 comments on commit 655802b

Please sign in to comment.