Skip to content

Commit

Permalink
Merge pull request #282 from andymandias/user-context-tweaks
Browse files Browse the repository at this point in the history
User Context Tweaks
  • Loading branch information
casperstorm committed Mar 20, 2024
2 parents 58a0492 + 45d5341 commit 2f1f82e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Changed:
- `enabled = bool`
- `smart = number`
- Use primary text color instead of accent color for `Solid` nicknames
- Op and Voice context menu items hidden in channels where the user is not an Op

# 2024.4 (2024-03-15)

Expand Down
29 changes: 19 additions & 10 deletions src/buffer/channel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use data::server::Server;
use data::user::Nick;
use data::User;
use data::{channel, history, message, Config};
use iced::widget::{column, container, row};
Expand Down Expand Up @@ -34,6 +35,10 @@ pub fn view<'a>(
let input = history.input(&buffer);
let our_nick = clients.nickname(&state.server);

let our_user = our_nick
.map(|our_nick| User::from(Nick::from(our_nick.as_ref())))
.and_then(|user| clients.resolve_user_attributes(&state.server, &state.channel, &user));

let messages = container(
scroll_view::view(
&state.scroll_view,
Expand Down Expand Up @@ -63,6 +68,7 @@ pub fn view<'a>(
),
user,
state.buffer(),
our_user,
)
.map(scroll_view::Message::UserContext);

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

let is_connected_to_channel = clients
.get_channels(&state.server)
.iter()
.any(|c| c == &state.channel);
let users = clients.get_channel_users(&state.server, &state.channel);
let channels = clients.get_channels(&state.server);
let nick_list = nick_list::view(users, &buffer, 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.
let topic = topic(state, clients, users, our_user, settings, config)
.unwrap_or_else(|| column![].into());

let show_text_input = match config.buffer.text_input.visibility {
data::buffer::TextInputVisibility::Focused => is_focused,
data::buffer::TextInputVisibility::Always => true,
};

// If topic toggles from None to Some then it messes with messages' scroll state,
// so produce a zero-height placeholder when topic is None.
let topic = topic(state, clients, users, settings, config).unwrap_or_else(|| column![].into());
let channels = clients.get_channels(&state.server);
let is_connected_to_channel = channels.iter().any(|c| c == &state.channel);

let text_input = show_text_input.then(move || {
input_view::view(
Expand Down Expand Up @@ -251,6 +257,7 @@ fn topic<'a>(
state: &'a Channel,
clients: &'a data::client::Map,
users: &'a [User],
our_user: Option<&'a User>,
settings: &'a channel::Settings,
config: &'a Config,
) -> Option<Element<'a, Message>> {
Expand All @@ -268,6 +275,7 @@ fn topic<'a>(
config.buffer.channel.topic.max_lines,
users,
&state.buffer(),
our_user,
config,
)
.map(Message::UserContext),
Expand All @@ -287,6 +295,7 @@ mod nick_list {
pub fn view<'a>(
users: &'a [User],
buffer: &Buffer,
our_user: Option<&'a User>,
config: &'a Config,
) -> Element<'a, Message> {
let column = column(users.iter().map(|user| {
Expand All @@ -298,7 +307,7 @@ mod nick_list {
)
});

user_context::view(content, user, buffer.clone())
user_context::view(content, user, buffer.clone(), our_user)
}))
.padding(4)
.spacing(1);
Expand Down
2 changes: 2 additions & 0 deletions src/buffer/channel/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn view<'a>(
max_lines: u16,
users: &'a [User],
buffer: &Buffer,
our_user: Option<&'a User>,
config: &'a Config,
) -> Element<'a, user_context::Message> {
let set_by = who.and_then(|who| {
Expand All @@ -31,6 +32,7 @@ pub fn view<'a>(
}),
user,
buffer.clone(),
our_user,
)
} else {
selectable_text(who)
Expand Down
1 change: 1 addition & 0 deletions src/buffer/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub fn view<'a>(
),
user,
state.buffer(),
None,
)
.map(scroll_view::Message::UserContext);

Expand Down
23 changes: 15 additions & 8 deletions src/buffer/user_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ enum Entry {
}

impl Entry {
fn list(buffer: &Buffer) -> Vec<Self> {
fn list(buffer: &Buffer, our_user: Option<&User>) -> Vec<Self> {
match buffer {
Buffer::Channel(_, _) => vec![
Entry::Whois,
Entry::Query,
Entry::ToggleAccessLevelOp,
Entry::ToggleAccessLevelVoice,
],
Buffer::Channel(_, _) => {
if our_user.is_some_and(|u| u.has_access_level(data::user::AccessLevel::Oper)) {
vec![
Entry::Whois,
Entry::Query,
Entry::ToggleAccessLevelOp,
Entry::ToggleAccessLevelVoice,
]
} else {
vec![Entry::Whois, Entry::Query]
}
}
Buffer::Server(_) | Buffer::Query(_, _) => vec![Entry::Whois],
}
}
Expand Down Expand Up @@ -56,8 +62,9 @@ pub fn view<'a>(
content: impl Into<Element<'a, Message>>,
user: &'a User,
buffer: Buffer,
our_user: Option<&'a User>,
) -> Element<'a, Message> {
let entries = Entry::list(&buffer);
let entries = Entry::list(&buffer, our_user);

let content = button(content)
.padding(0)
Expand Down

0 comments on commit 2f1f82e

Please sign in to comment.