1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:17:35 +00:00

LibGUI: Add GTableModel::Role::ForegroundColor.

This makes it possible to specify the text color for each table cell.
Use this to make the IRCClient show unread window list items in red.
This commit is contained in:
Andreas Kling 2019-03-18 04:54:07 +01:00
parent f4b8e4966f
commit d466f2634d
10 changed files with 120 additions and 63 deletions

View file

@ -151,18 +151,18 @@ GVariant DirectoryTableModel::data(const GModelIndex& index, Role role) const
}
ASSERT_NOT_REACHED();
}
ASSERT(role == Role::Display);
switch (index.column()) {
case Column::Icon: return icon_for(entry);
case Column::Name: return entry.name;
case Column::Size: return (int)entry.size;
case Column::Owner: return name_for_uid(entry.uid);
case Column::Group: return name_for_gid(entry.gid);
case Column::Permissions: return permission_string(entry.mode);
case Column::Inode: return (int)entry.inode;
if (role == Role::Display) {
switch (index.column()) {
case Column::Icon: return icon_for(entry);
case Column::Name: return entry.name;
case Column::Size: return (int)entry.size;
case Column::Owner: return name_for_uid(entry.uid);
case Column::Group: return name_for_gid(entry.gid);
case Column::Permissions: return permission_string(entry.mode);
case Column::Inode: return (int)entry.inode;
}
}
ASSERT_NOT_REACHED();
return { };
}
void DirectoryTableModel::update()

View file

@ -120,7 +120,7 @@ void IRCAppWindow::setup_widgets()
m_window_list->set_alternating_row_colors(false);
m_window_list->set_model(OwnPtr<IRCWindowListModel>(m_client.client_window_list_model()));
m_window_list->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
m_window_list->set_preferred_size({ 120, 0 });
m_window_list->set_preferred_size({ 100, 0 });
m_client.client_window_list_model()->on_activation = [this] (IRCWindow& window) {
m_container->set_active_widget(&window);
window.clear_unread_count();

View file

@ -39,12 +39,14 @@ GTableModel::ColumnMetadata IRCChannelMemberListModel::column_metadata(int colum
ASSERT_NOT_REACHED();
}
GVariant IRCChannelMemberListModel::data(const GModelIndex& index, Role) const
GVariant IRCChannelMemberListModel::data(const GModelIndex& index, Role role) const
{
switch (index.column()) {
case Column::Name: return m_channel.member_at(index.row());
if (role == Role::Display) {
switch (index.column()) {
case Column::Name: return m_channel.member_at(index.row());
}
}
ASSERT_NOT_REACHED();
return { };
}
void IRCChannelMemberListModel::update()

View file

@ -43,21 +43,23 @@ GTableModel::ColumnMetadata IRCLogBufferModel::column_metadata(int column) const
ASSERT_NOT_REACHED();
}
GVariant IRCLogBufferModel::data(const GModelIndex& index, Role) const
GVariant IRCLogBufferModel::data(const GModelIndex& index, Role role) const
{
auto& entry = m_log_buffer->at(index.row());
switch (index.column()) {
case Column::Timestamp: {
auto* tm = localtime(&entry.timestamp);
return String::format("%02u:%02u:%02u", tm->tm_hour, tm->tm_min, tm->tm_sec);
if (role == Role::Display) {
auto& entry = m_log_buffer->at(index.row());
switch (index.column()) {
case Column::Timestamp: {
auto* tm = localtime(&entry.timestamp);
return String::format("%02u:%02u:%02u", tm->tm_hour, tm->tm_min, tm->tm_sec);
}
case Column::Name:
if (entry.sender.is_empty())
return String::empty();
return String::format("<%c%s>", entry.prefix ? entry.prefix : ' ', entry.sender.characters());
case Column::Text: return entry.text;
}
}
case Column::Name:
if (entry.sender.is_empty())
return String::empty();
return String::format("<%c%s>", entry.prefix ? entry.prefix : ' ', entry.sender.characters());
case Column::Text: return entry.text;
}
ASSERT_NOT_REACHED();
return { };
}
void IRCLogBufferModel::update()

View file

@ -40,17 +40,29 @@ GTableModel::ColumnMetadata IRCWindowListModel::column_metadata(int column) cons
ASSERT_NOT_REACHED();
}
GVariant IRCWindowListModel::data(const GModelIndex& index, Role) const
GVariant IRCWindowListModel::data(const GModelIndex& index, Role role) const
{
switch (index.column()) {
case Column::Name: {
auto& window = m_client.window_at(index.row());
if (!window.unread_count())
if (role == Role::Display) {
switch (index.column()) {
case Column::Name: {
auto& window = m_client.window_at(index.row());
if (window.unread_count())
return String::format("%s (%d)", window.name().characters(), window.unread_count());
return window.name();
return String::format("%s (%d)\n", window.name().characters(), window.unread_count());
}
}
}
if (role == Role::ForegroundColor) {
switch (index.column()) {
case Column::Name: {
auto& window = m_client.window_at(index.row());
if (window.unread_count())
return Color(Color::Red);
return Color(Color::Black);
}
}
}
ASSERT_NOT_REACHED();
return { };
}
void IRCWindowListModel::update()

View file

@ -99,25 +99,28 @@ GVariant ProcessTableModel::data(const GModelIndex& index, Role role) const
return { };
}
switch (index.column()) {
case Column::Icon: return *m_generic_process_icon;
case Column::PID: return process.current_state.pid;
case Column::State: return process.current_state.state;
case Column::User: return process.current_state.user;
case Column::Priority:
if (process.current_state.priority == "High")
return *m_high_priority_icon;
if (process.current_state.priority == "Low")
return *m_low_priority_icon;
if (process.current_state.priority == "Normal")
return *m_normal_priority_icon;
return process.current_state.priority;
case Column::Linear: return pretty_byte_size(process.current_state.linear);
case Column::Physical: return pretty_byte_size(process.current_state.physical);
case Column::CPU: return process.current_state.cpu_percent;
case Column::Name: return process.current_state.name;
if (role == Role::Display) {
switch (index.column()) {
case Column::Icon: return *m_generic_process_icon;
case Column::PID: return process.current_state.pid;
case Column::State: return process.current_state.state;
case Column::User: return process.current_state.user;
case Column::Priority:
if (process.current_state.priority == "High")
return *m_high_priority_icon;
if (process.current_state.priority == "Low")
return *m_low_priority_icon;
if (process.current_state.priority == "Normal")
return *m_normal_priority_icon;
return process.current_state.priority;
case Column::Linear: return pretty_byte_size(process.current_state.linear);
case Column::Physical: return pretty_byte_size(process.current_state.physical);
case Column::CPU: return process.current_state.cpu_percent;
case Column::Name: return process.current_state.name;
}
}
ASSERT_NOT_REACHED();
return { };
}
void ProcessTableModel::update()