Skip to content

Commit

Permalink
Add scroll to backlog / bottom button
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Oct 4, 2024
1 parent 7bd190e commit 1007f83
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ impl Buffer {
.map(|message| Message::Highlights(highlights::Message::ScrollView(message))),
}
}

pub fn is_scrolled_to_bottom(&self) -> Option<bool> {
match self {
Buffer::Empty | Buffer::FileTransfers(_) => None,
Buffer::Channel(channel) => Some(channel.scroll_view.is_scrolled_to_bottom()),
Buffer::Server(server) => Some(server.scroll_view.is_scrolled_to_bottom()),
Buffer::Query(query) => Some(query.scroll_view.is_scrolled_to_bottom()),
Buffer::Logs(log) => Some(log.scroll_view.is_scrolled_to_bottom()),
Buffer::Highlights(highlights) => Some(highlights.scroll_view.is_scrolled_to_bottom()),
}
}
}

impl From<data::Buffer> for Buffer {
Expand Down
4 changes: 4 additions & 0 deletions src/buffer/scroll_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ impl State {
)
}

pub fn is_scrolled_to_bottom(&self) -> bool {
matches!(self.status, Status::Idle(Anchor::Bottom))
}

pub fn scroll_to_message(
&mut self,
message: message::Hash,
Expand Down
29 changes: 29 additions & 0 deletions src/screen/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,35 @@ impl Dashboard {
pane::Message::MaximizePane => self.maximize_pane(),
pane::Message::Popout => return (self.popout_pane(main_window), None),
pane::Message::Merge => return (self.merge_pane(config, main_window), None),
pane::Message::ScrollToBacklog => {
if let Some((window, pane)) = self.focus {
if let Some(state) = self.panes.get_mut(main_window.id, window, pane) {
return (
state.buffer.scroll_to_backlog(&self.history, config).map(
move |message| {
Message::Pane(
window,
pane::Message::Buffer(pane, message),
)
},
),
None,
);
}
}
}
pane::Message::ScrollToBottom => {
if let Some((window, pane)) = self.focus {
if let Some(state) = self.panes.get_mut(main_window.id, window, pane) {
return (
state.buffer.scroll_to_end().map(move |message| {
Message::Pane(window, pane::Message::Buffer(pane, message))
}),
None,
);
}
}
}
}
}
Message::Sidebar(message) => {
Expand Down
25 changes: 25 additions & 0 deletions src/screen/dashboard/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum Message {
ToggleShowTopic,
Popout,
Merge,
ScrollToBacklog,
ScrollToBottom,
}

#[derive(Clone)]
Expand Down Expand Up @@ -162,6 +164,29 @@ impl TitleBar {
let mut controls = row![].spacing(2);

if let Buffer::Channel(state) = &buffer {
// Show scroll-to-bottom / scroll-to-backlog depending if scrollable is anchored to the end or not
let (icon, tooltip_text, message) =
if buffer.is_scrolled_to_bottom().unwrap_or_default() {
(icon::dot(), "Scroll to Backlog", Message::ScrollToBacklog)
} else {
(icon::cancel(), "Scroll to Bottom", Message::ScrollToBottom)
};

let scrollable_button = button(center(icon))
.padding(5)
.width(22)
.height(22)
.on_press(message)
.style(|theme, status| theme::button::secondary(theme, status, false));

let scrollable_button_with_tooltip = tooltip(
scrollable_button,
show_tooltips.then_some(tooltip_text),
tooltip::Position::Bottom,
);

controls = controls.push(scrollable_button_with_tooltip);

// Show topic button only if there is a topic to show
if let Some(topic) = clients.get_channel_topic(&state.server, &state.channel) {
if topic.content.is_some() {
Expand Down

0 comments on commit 1007f83

Please sign in to comment.