Skip to content

Commit

Permalink
use styled button for channels
Browse files Browse the repository at this point in the history
less bloat
  • Loading branch information
exp111 committed Nov 26, 2023
1 parent 42f32bc commit c8df9f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
9 changes: 8 additions & 1 deletion Turbulence.Core/ViewModels/ChannelListViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Numerics;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Turbulence.Discord;
using Turbulence.Discord.Models;
Expand All @@ -26,7 +27,13 @@ public ChannelListViewModel()
Messenger.Send(new ChannelSelectedMsg(channel));
};
}


[RelayCommand]
public void SelectChannel(Channel channel)
{
SelectedChannel = channel;
}

public void Receive(SetChannelsMsg m) => Channels.ReplaceAll(m.Channels);

//TODO: move to client
Expand Down
13 changes: 7 additions & 6 deletions Turbulence.Desktop/Views/Main/ChannelListView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<converters:ObjectEqualsMultiConverter x:Key="EqualsConverter" />
</UserControl.Resources>
<UserControl.Styles>
<Style Selector="ToggleButton">
<Style Selector="Button">
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="ToggleButton:checked /template/ ContentPresenter#PART_ContentPresenter">
<Style Selector="Button.Checked /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="#404249" />
</Style>
<Style Selector="TextBlock.Category">
Expand All @@ -33,14 +33,15 @@
<dataTemplates:ChannelTemplateSelector>
<!--Channels that can be clicked-->
<DataTemplate x:Key="channel" x:DataType="channel:Channel">
<ToggleButton Click="ChannelSelected"
<Button Command="{Binding $parent[ItemsControl].((vm:ChannelListViewModel)DataContext).SelectChannelCommand}"
CommandParameter="{Binding .}"
HorizontalAlignment="Stretch" Margin="4,1">
<ToggleButton.IsChecked>
<Classes.Checked>
<MultiBinding Converter="{StaticResource EqualsConverter}">
<Binding Path="$parent[ItemsControl].((vm:ChannelListViewModel)DataContext).SelectedChannel" />
<Binding Path="." />
</MultiBinding>
</ToggleButton.IsChecked>
</Classes.Checked>
<Grid ColumnDefinitions="20,*">
<!--The icon-->
<ContentControl Content="{Binding}">
Expand All @@ -67,7 +68,7 @@
<!--Channel Name-->
<TextBlock Text="{Binding Name}" Grid.Column="1" Margin="5,0" />
</Grid>
</ToggleButton>
</Button>
</DataTemplate>
<!--Category-->
<DataTemplate x:Key="category" x:DataType="channel:Channel">
Expand Down
22 changes: 0 additions & 22 deletions Turbulence.Desktop/Views/Main/ChannelListView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Turbulence.Core.ViewModels;
using Turbulence.Core.ViewModels.Design;
using Turbulence.Discord.Models.DiscordChannel;

namespace Turbulence.Desktop.Views.Main;

public partial class ChannelListView : UserControl
{
private ChannelListViewModel _vm;
public ChannelListView()
{
InitializeComponent();
Expand All @@ -18,22 +13,5 @@ public ChannelListView()
// Workaround to fix design data context getting overwritten
DataContext = new DesignChannelListViewModel();
}
_vm = (ChannelListViewModel)DataContext!;
}

//TODO: use a button with a "checked" class + command to do this
public void ChannelSelected(object sender, RoutedEventArgs e)
{
if (e.Source is ToggleButton control)
{
var dataContext = control.DataContext;
if (dataContext is Channel)
{
if (_vm.SelectedChannel != (Channel)dataContext)
_vm.SelectedChannel = (Channel)dataContext;
else // if its already selected, then pls check this one again
control.IsChecked = true;
}
}
}
}

0 comments on commit c8df9f4

Please sign in to comment.