-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Notification::Menu): Implement property updated for the Data…
…Container of the menu item
- Loading branch information
Showing
7 changed files
with
180 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
11 changes: 6 additions & 5 deletions
11
...rms/Components/AudioDeviceBox.Designer.cs → ...Forms/Components/IconMenuItem.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Drawing; | ||
using System.Runtime.CompilerServices; | ||
using System.Windows.Forms; | ||
using JetBrains.Annotations; | ||
using SoundSwitch.Util; | ||
|
||
namespace SoundSwitch.UI.Forms.Components | ||
{ | ||
public partial class IconMenuItem : UserControl | ||
{ | ||
public DataContainer CurrentDataContainer { get; } | ||
|
||
public class DataContainer : INotifyPropertyChanged | ||
{ | ||
private bool _selected; | ||
private Image _image; | ||
private string _label; | ||
private Icon _icon; | ||
|
||
public bool Selected | ||
{ | ||
get => _selected; | ||
set | ||
{ | ||
_selected = value; | ||
OnPropertyChanged(); | ||
OnPropertyChanged(nameof(Color)); | ||
} | ||
} | ||
|
||
public Image Image | ||
{ | ||
get { return _image ??= _icon.ToBitmap(); } | ||
} | ||
|
||
public string Label | ||
{ | ||
get => _label; | ||
set | ||
{ | ||
_label = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public Icon Icon | ||
{ | ||
get => _icon; | ||
set | ||
{ | ||
_icon = value; | ||
OnPropertyChanged(); | ||
var oldImage = _image; | ||
_image = null; | ||
OnPropertyChanged(nameof(Image)); | ||
oldImage?.Dispose(); | ||
} | ||
} | ||
|
||
public string Id { get; } | ||
public Color Color => Selected ? Color.RoyalBlue.WithOpacity(0x80) : Color.Black.WithOpacity(0x70); | ||
|
||
public DataContainer(Icon icon, string label, bool selected, string id) | ||
{ | ||
Selected = selected; | ||
Icon = icon; | ||
Label = label; | ||
Id = id; | ||
} | ||
|
||
/// <summary> | ||
/// Override the metadata part | ||
/// </summary> | ||
/// <param name="dataContainer"/> | ||
public void OverrideMetadata(DataContainer dataContainer) | ||
{ | ||
if (dataContainer.Id != Id) | ||
{ | ||
throw new ArgumentException("Need to have the same ID", nameof(dataContainer)); | ||
} | ||
|
||
if (Selected != dataContainer.Selected) | ||
Selected = dataContainer.Selected; | ||
if (Icon != dataContainer.Icon) | ||
Icon = dataContainer.Icon; | ||
if (Label != dataContainer.Label) | ||
Label = dataContainer.Label; | ||
} | ||
|
||
public event PropertyChangedEventHandler PropertyChanged; | ||
|
||
[NotifyPropertyChangedInvocator] | ||
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) | ||
{ | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
} | ||
} | ||
|
||
|
||
public IconMenuItem(DataContainer dataContainer) | ||
{ | ||
CurrentDataContainer = dataContainer; | ||
InitializeComponent(); | ||
|
||
base.CreateParams.ExStyle |= 0x20; | ||
SetStyle(ControlStyles.SupportsTransparentBackColor, true); | ||
iconBox.BackColor = Color.Transparent; | ||
deviceName.BackColor = Color.Transparent; | ||
Name = CurrentDataContainer.Id; | ||
|
||
iconBox.DataBindings.Add(nameof(PictureBox.Image), CurrentDataContainer, nameof(CurrentDataContainer.Image), false, DataSourceUpdateMode.OnPropertyChanged); | ||
deviceName.DataBindings.Add(nameof(Label.Text), CurrentDataContainer, nameof(CurrentDataContainer.Label), false, DataSourceUpdateMode.OnPropertyChanged); | ||
DataBindings.Add(nameof(BackColor), CurrentDataContainer, nameof(CurrentDataContainer.Color), false, DataSourceUpdateMode.OnPropertyChanged); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters