Skip to content

Commit

Permalink
Add context menu to user fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Sep 16, 2024
1 parent a0d4275 commit e62a9dc
Show file tree
Hide file tree
Showing 16 changed files with 596 additions and 326 deletions.
10 changes: 5 additions & 5 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ impl Buffer {
Self::Empty
}

pub fn data(&self) -> Option<data::Buffer> {
pub fn data(&self) -> Option<&data::Buffer> {
match self {
Buffer::Empty => None,
Buffer::Channel(state) => Some(state.buffer()),
Buffer::Server(state) => Some(state.buffer()),
Buffer::Query(state) => Some(state.buffer()),
Buffer::Channel(state) => Some(&state.buffer),
Buffer::Server(state) => Some(&state.buffer),
Buffer::Query(state) => Some(&state.buffer),
Buffer::FileTransfers(_) => None,
}
}
Expand Down Expand Up @@ -177,7 +177,7 @@ impl Buffer {
nick: Nick,
history: &mut history::Manager,
) -> Task<Message> {
if let Some(buffer) = self.data() {
if let Some(buffer) = self.data().cloned() {
match self {
Buffer::Empty | Buffer::Server(_) | Buffer::FileTransfers(_) => Task::none(),
Buffer::Channel(channel) => channel
Expand Down
52 changes: 28 additions & 24 deletions src/buffer/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub fn view<'a>(
theme: &'a Theme,
is_focused: bool,
) -> Element<'a, Message> {
let buffer = state.buffer();
let input = history.input(&buffer);
let buffer = &state.buffer;
let input = history.input(buffer);
let our_nick = clients.nickname(&state.server);

let our_user = our_nick
Expand Down Expand Up @@ -88,6 +88,8 @@ pub fn view<'a>(

match message.target.source() {
message::Source::User(user) => {
let current_user = users.iter().find(|current_user| *current_user == user);

let mut text = selectable_text(
config
.buffer
Expand All @@ -109,20 +111,26 @@ pub fn view<'a>(
.horizontal_alignment(alignment::Horizontal::Right);
}

let nick = user_context::view(
text,
user,
users.iter().find(|current_user| *current_user == user),
state.buffer(),
our_user,
)
.map(scroll_view::Message::UserContext);
let nick = user_context::view(text, user, current_user, buffer, our_user)
.map(scroll_view::Message::UserContext);

let text = message_content(
let text = message_content::with_context(
&message.content,
theme,
scroll_view::Message::Link,
theme::selectable_text::default,
move |link| match link {
message::Link::Url(_) => vec![],
message::Link::User(_) => {
user_context::Entry::list(buffer, our_user)
}
},
move |link, entry, length| match link {
message::Link::Url(_) => row![].into(),
message::Link::User(user) => entry
.view(user, current_user, length)
.map(scroll_view::Message::UserContext),
},
config,
);

Expand Down Expand Up @@ -235,7 +243,7 @@ pub fn view<'a>(
.width(Length::FillPortion(2))
.height(Length::Fill);

let nick_list = nick_list::view(users, &buffer, our_user, config).map(Message::UserContext);
let nick_list = nick_list::view(users, buffer, our_user, config).map(Message::UserContext);

// If topic toggles from None to Some then it messes with messages' scroll state,
// so produce a zero-height placeholder when topic is None.
Expand Down Expand Up @@ -287,6 +295,7 @@ pub fn view<'a>(

#[derive(Debug, Clone)]
pub struct Channel {
pub buffer: data::Buffer,
pub server: Server,
pub channel: String,

Expand All @@ -297,17 +306,14 @@ pub struct Channel {
impl Channel {
pub fn new(server: Server, channel: String) -> Self {
Self {
buffer: data::Buffer::Channel(server.clone(), channel.clone()),
server,
channel,
scroll_view: scroll_view::State::new(),
input_view: input_view::State::new(),
}
}

pub fn buffer(&self) -> data::Buffer {
data::Buffer::Channel(self.server.clone(), self.channel.clone())
}

pub fn update(
&mut self,
message: Message,
Expand All @@ -326,11 +332,9 @@ impl Channel {
(command.map(Message::ScrollView), event)
}
Message::InputView(message) => {
let buffer = self.buffer();

let (command, event) = self
.input_view
.update(message, buffer, clients, history, config);
let (command, event) =
self.input_view
.update(message, &self.buffer, clients, history, config);
let command = command.map(Message::InputView);

match event {
Expand Down Expand Up @@ -383,7 +387,7 @@ fn topic<'a>(
topic.time.as_ref(),
config.buffer.channel.topic.max_lines,
users,
&state.buffer(),
&state.buffer,
our_user,
config,
theme,
Expand All @@ -404,7 +408,7 @@ mod nick_list {

pub fn view<'a>(
users: &'a [User],
buffer: &Buffer,
buffer: &'a Buffer,
our_user: Option<&'a User>,
config: &'a Config,
) -> Element<'a, Message> {
Expand Down Expand Up @@ -442,7 +446,7 @@ mod nick_list {
})
.width(Length::Fixed(width));

user_context::view(content, user, Some(user), buffer.clone(), our_user)
user_context::view(content, user, Some(user), buffer, our_user)
}));

Scrollable::new(content)
Expand Down
4 changes: 2 additions & 2 deletions src/buffer/channel/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn view<'a>(
time: Option<&'a DateTime<Utc>>,
max_lines: u16,
users: &'a [User],
buffer: &Buffer,
buffer: &'a Buffer,
our_user: Option<&'a User>,
config: &'a Config,
theme: &'a Theme,
Expand All @@ -33,7 +33,7 @@ pub fn view<'a>(
}),
user,
Some(user),
buffer.clone(),
buffer,
our_user,
)
} else {
Expand Down
21 changes: 12 additions & 9 deletions src/buffer/input_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl State {
pub fn update(
&mut self,
message: Message,
buffer: Buffer,
buffer: &Buffer,
clients: &mut client::Map,
history: &mut history::Manager,
config: &Config,
Expand All @@ -139,14 +139,14 @@ impl State {
self.completion.process(&input, users, channels, &isupport);

history.record_draft(Draft {
buffer,
buffer: buffer.clone(),
text: input,
});

(Task::none(), None)
}
Message::Send => {
let input = history.input(&buffer).draft;
let input = history.input(buffer).draft;

// Reset error
self.error = None;
Expand Down Expand Up @@ -174,7 +174,7 @@ impl State {
};

if let Some(encoded) = input.encoded() {
clients.send(&buffer, encoded);
clients.send(buffer, encoded);
}

if let Some(nick) = clients.nickname(buffer.server()) {
Expand All @@ -201,7 +201,7 @@ impl State {
}
}
Message::Tab(reverse) => {
let input = history.input(&buffer).draft;
let input = history.input(buffer).draft;

if let Some(entry) = self.completion.tab(reverse) {
let new_input = entry.complete_input(input);
Expand All @@ -212,7 +212,7 @@ impl State {
}
}
Message::Up => {
let cache = history.input(&buffer);
let cache = history.input(buffer);

self.completion.reset();

Expand Down Expand Up @@ -245,7 +245,7 @@ impl State {
(Task::none(), None)
}
Message::Down => {
let cache = history.input(&buffer);
let cache = history.input(buffer);

self.completion.reset();

Expand Down Expand Up @@ -279,11 +279,14 @@ impl State {

fn on_completion(
&self,
buffer: Buffer,
buffer: &Buffer,
history: &mut history::Manager,
text: String,
) -> (Task<Message>, Option<Event>) {
history.record_draft(Draft { buffer, text });
history.record_draft(Draft {
buffer: buffer.clone(),
text,
});

(text_input::move_cursor_to_end(self.input_id.clone()), None)
}
Expand Down
32 changes: 19 additions & 13 deletions src/buffer/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub fn view<'a>(
is_focused: bool,
) -> Element<'a, Message> {
let status = clients.status(&state.server);
let buffer = state.buffer();
let input = history.input(&buffer);
let buffer = &state.buffer;
let input = history.input(buffer);

let messages = container(
scroll_view::view(
Expand Down Expand Up @@ -71,14 +71,24 @@ pub fn view<'a>(
.horizontal_alignment(alignment::Horizontal::Right);
}

let nick = user_context::view(text, user, None, state.buffer(), None)
let nick = user_context::view(text, user, None, buffer, None)
.map(scroll_view::Message::UserContext);

let message = message_content(
let message = message_content::with_context(
&message.content,
theme,
scroll_view::Message::Link,
theme::selectable_text::default,
move |link| match link {
message::Link::Url(_) => vec![],
message::Link::User(_) => user_context::Entry::list(buffer, None),
},
move |link, entry, length| match link {
message::Link::Url(_) => row![].into(),
message::Link::User(user) => entry
.view(user, None, length)
.map(scroll_view::Message::UserContext),
},
config,
);

Expand Down Expand Up @@ -201,6 +211,7 @@ pub fn view<'a>(

#[derive(Debug, Clone)]
pub struct Query {
pub buffer: data::Buffer,
pub server: Server,
pub nick: Nick,
pub scroll_view: scroll_view::State,
Expand All @@ -210,17 +221,14 @@ pub struct Query {
impl Query {
pub fn new(server: Server, nick: Nick) -> Self {
Self {
buffer: data::Buffer::Query(server.clone(), nick.clone()),
server,
nick,
scroll_view: scroll_view::State::new(),
input_view: input_view::State::new(),
}
}

pub fn buffer(&self) -> data::Buffer {
data::Buffer::Query(self.server.clone(), self.nick.clone())
}

pub fn update(
&mut self,
message: Message,
Expand All @@ -239,11 +247,9 @@ impl Query {
(command.map(Message::ScrollView), event)
}
Message::InputView(message) => {
let buffer = self.buffer();

let (command, event) = self
.input_view
.update(message, buffer, clients, history, config);
let (command, event) =
self.input_view
.update(message, &self.buffer, clients, history, config);
let command = command.map(Message::InputView);

match event {
Expand Down
18 changes: 7 additions & 11 deletions src/buffer/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub fn view<'a>(
is_focused: bool,
) -> Element<'a, Message> {
let status = clients.status(&state.server);
let buffer = state.buffer();
let input = history.input(&buffer);
let buffer = &state.buffer;
let input = history.input(buffer);

let messages = container(
scroll_view::view(
Expand Down Expand Up @@ -97,6 +97,7 @@ pub fn view<'a>(

#[derive(Debug, Clone)]
pub struct Server {
pub buffer: data::Buffer,
pub server: data::server::Server,
pub scroll_view: scroll_view::State,
pub input_view: input_view::State,
Expand All @@ -105,16 +106,13 @@ pub struct Server {
impl Server {
pub fn new(server: data::server::Server) -> Self {
Self {
buffer: data::Buffer::Server(server.clone()),
server,
scroll_view: scroll_view::State::new(),
input_view: input_view::State::new(),
}
}

pub fn buffer(&self) -> data::Buffer {
data::Buffer::Server(self.server.clone())
}

pub fn update(
&mut self,
message: Message,
Expand All @@ -128,11 +126,9 @@ impl Server {
command.map(Message::ScrollView)
}
Message::InputView(message) => {
let buffer = self.buffer();

let (command, event) = self
.input_view
.update(message, buffer, clients, history, config);
let (command, event) =
self.input_view
.update(message, &self.buffer, clients, history, config);
let command = command.map(Message::InputView);

match event {
Expand Down
Loading

0 comments on commit e62a9dc

Please sign in to comment.