diff --git a/EstusShots.Gtk/Dialogs/DialogBase.cs b/EstusShots.Gtk/Dialogs/DialogBase.cs index 3b75924..b92b55a 100644 --- a/EstusShots.Gtk/Dialogs/DialogBase.cs +++ b/EstusShots.Gtk/Dialogs/DialogBase.cs @@ -18,47 +18,33 @@ namespace EstusShots.Gtk.Dialogs } public delegate void DialogClosedEventHandler(object o, DialogClosedEventArgs args) where T: class, new(); - // TODO remove non-generic version - public class DialogClosedEventArgs : EventArgs - { - public bool Ok { get; } - public object Model { get; } - - public DialogClosedEventArgs(bool ok, object model) - { - Ok = ok; - Model = model; - } - } - public delegate void DialogClosedEventHandler(object o, DialogClosedEventArgs args); - - public abstract class DialogBase where T: class, new() + public abstract class DialogBase : Dialog where T: class, new() { + protected readonly Builder Builder; protected T EditObject { get; set; } - [UI] private readonly Dialog _editorDialog = null; [UI] private readonly Button _saveButton = null; [UI] private readonly Button _cancelButton = null; - public event DialogClosedEventHandler OnDialogClosed; + public event DialogClosedEventHandler DialogClosed; - protected DialogBase(Window parent, Builder builder) + protected DialogBase(Window parent, Builder builder) : base(builder.GetObject("_editorDialog").Handle) { - builder.Autoconnect(this); - + Builder = builder; + Builder.Autoconnect(this); _saveButton.Clicked += OnSaveButtonClicked; _cancelButton.Clicked += (sender, args) => { - OnDialogClosed?.Invoke(this, new DialogClosedEventArgs(false, new T())); - _editorDialog.Dispose(); + DialogClosed?.Invoke(this, new DialogClosedEventArgs(false, new T())); + Dispose(); }; - _editorDialog.TransientFor = parent; + TransientFor = parent; } - public void Show() + public new void Show() { LoadFromModel(); - _editorDialog.Show(); + base.Show(); } private void OnSaveButtonClicked(object sender, EventArgs e) @@ -66,8 +52,8 @@ namespace EstusShots.Gtk.Dialogs try { LoadToModel(); - OnDialogClosed?.Invoke(this, new DialogClosedEventArgs(true, EditObject)); - _editorDialog.Dispose(); + DialogClosed?.Invoke(this, new DialogClosedEventArgs(true, EditObject)); + Dispose(); } catch (Exception exception) { diff --git a/EstusShots.Gtk/Dialogs/Dialogs.glade b/EstusShots.Gtk/Dialogs/Dialogs.glade index 162c9b2..9634ff1 100644 --- a/EstusShots.Gtk/Dialogs/Dialogs.glade +++ b/EstusShots.Gtk/Dialogs/Dialogs.glade @@ -2,137 +2,7 @@ - - 100 - 0.10000000000000001 - 10 - - - False - Drink - False - True - center-on-parent - True - dialog - center - - - - - - False - vertical - 2 - - - False - end - - - gtk-save - True - True - True - True - - - - True - True - 0 - - - - - gtk-cancel - True - True - True - True - - - True - True - 1 - - - - - False - False - 0 - - - - - True - False - 10 - 10 - 5 - 7 - - - True - False - end - Name - - - 0 - 0 - - - - - True - False - end - Vol. Alcohol - - - 0 - 1 - - - - - True - True - - - 1 - 0 - - - - - True - True - % - number - DrinkVolAdjustment - 0.10000000000000001 - 1 - True - - - 1 - 1 - - - - - False - True - 1 - - - - - - + False Player False @@ -154,7 +24,7 @@ False end - + gtk-save True True @@ -169,7 +39,7 @@ - + gtk-cancel True True diff --git a/EstusShots.Gtk/Dialogs/DrinkEditor.cs b/EstusShots.Gtk/Dialogs/DrinkEditor.cs index 272eaad..ad4f554 100644 --- a/EstusShots.Gtk/Dialogs/DrinkEditor.cs +++ b/EstusShots.Gtk/Dialogs/DrinkEditor.cs @@ -1,63 +1,31 @@ -using System; using EstusShots.Shared.Dto; using Gtk; using UI = Gtk.Builder.ObjectAttribute; namespace EstusShots.Gtk.Dialogs { - public class DrinkEditor + public class DrinkEditor : DialogBase { - [UI] private readonly Dialog DrinkEditorDialog = null; - [UI] private readonly Entry DrinkNameEntry = null; - [UI] private readonly Adjustment DrinkVolAdjustment = null; - [UI] private readonly Button SaveDrinkButton = null; - [UI] private readonly Button CancelDrinkEditorButton = null; - - private readonly Drink _drink; - - public event DialogClosedEventHandler OnDialogClosed; + [UI] private readonly Entry _nameEntry = null; + [UI] private readonly Adjustment _volAdjustment = null; - public DrinkEditor(Window parent, Drink drink) + public DrinkEditor(Window parent, Drink drink) : base(parent, new Builder("DrinkEditor.glade")) { - _drink = drink; - + EditObject = drink; var builder = new Builder("Dialogs.glade"); builder.Autoconnect(this); - - SaveDrinkButton.Clicked += SaveDrinkButtonOnClicked; - CancelDrinkEditorButton.Clicked += (sender, args) => - { - OnDialogClosed?.Invoke(this, new DialogClosedEventArgs(false, null)); - DrinkEditorDialog.Dispose(); - }; - - ReadFromModel(); - - DrinkEditorDialog.TransientFor = parent; - DrinkEditorDialog.Show(); - } - - // Events - - private void SaveDrinkButtonOnClicked(object sender, EventArgs e) - { - ReadToModel(); - OnDialogClosed?.Invoke(this, new DialogClosedEventArgs(true, _drink)); - DrinkEditorDialog.Dispose(); } - // Private Methods - - private void ReadToModel() + protected override void LoadToModel() { - _drink.Name = DrinkNameEntry.Text; - _drink.Vol = DrinkVolAdjustment.Value; + EditObject.Name = _nameEntry.Text; + EditObject.Vol = _volAdjustment.Value; } - private void ReadFromModel() + protected override void LoadFromModel() { - DrinkNameEntry.Text = _drink.Name; - DrinkVolAdjustment.Value = _drink.Vol; + _nameEntry.Text = EditObject.Name; + _volAdjustment.Value = EditObject.Vol; } } } \ No newline at end of file diff --git a/EstusShots.Gtk/Dialogs/Glade/DrinkEditor.glade b/EstusShots.Gtk/Dialogs/Glade/DrinkEditor.glade new file mode 100644 index 0000000..ec39b00 --- /dev/null +++ b/EstusShots.Gtk/Dialogs/Glade/DrinkEditor.glade @@ -0,0 +1,136 @@ + + + + + + 100 + 0.10000000000000001 + 10 + + + False + Drink + False + True + center-on-parent + True + dialog + center + + + + + + False + vertical + 2 + + + False + end + + + gtk-save + True + True + True + True + + + + True + True + 0 + + + + + gtk-cancel + True + True + True + True + + + True + True + 1 + + + + + False + False + 0 + + + + + True + False + 10 + 10 + 5 + 7 + + + True + False + end + Name + + + 0 + 0 + + + + + True + False + end + Vol. Alcohol + + + 0 + 1 + + + + + True + True + + + 1 + 0 + + + + + True + True + 0,0 + % + number + _volAdjustment + 0.10000000000000001 + 1 + True + + + 1 + 1 + + + + + False + True + 1 + + + + + + diff --git a/EstusShots.Gtk/Dialogs/Glade/PlayerEditor.glade b/EstusShots.Gtk/Dialogs/Glade/PlayerEditor.glade new file mode 100644 index 0000000..cdd125f --- /dev/null +++ b/EstusShots.Gtk/Dialogs/Glade/PlayerEditor.glade @@ -0,0 +1,165 @@ + + + + + + False + Player + False + True + center-on-parent + True + dialog + center + + + + + + False + vertical + 2 + + + False + end + + + gtk-save + True + True + True + True + + + + True + True + 0 + + + + + gtk-cancel + True + True + True + True + + + True + True + 1 + + + + + False + False + 0 + + + + + True + False + 10 + 10 + 5 + 7 + + + True + False + end + Name + + + 0 + 0 + + + + + True + False + end + Alias + + + 0 + 1 + + + + + True + True + + + 1 + 0 + + + + + True + True + + + 1 + 1 + + + + + Anonymous + True + True + False + True + + + 1 + 3 + + + + + True + False + end + Hex ID + + + 0 + 2 + + + + + True + True + 4 + 0x00 + alpha + + + 1 + 2 + + + + + + + + True + True + 1 + + + + + + diff --git a/EstusShots.Gtk/Dialogs/PlayerEditor.cs b/EstusShots.Gtk/Dialogs/PlayerEditor.cs index bd41dd0..cc2cb61 100644 --- a/EstusShots.Gtk/Dialogs/PlayerEditor.cs +++ b/EstusShots.Gtk/Dialogs/PlayerEditor.cs @@ -1,69 +1,35 @@ -using System; using EstusShots.Shared.Dto; using Gtk; using UI = Gtk.Builder.ObjectAttribute; namespace EstusShots.Gtk.Dialogs { - public class PlayerEditor + public class PlayerEditor : DialogBase { - private readonly Player _player; - - [UI] private readonly Dialog PlayerEditorDialog = null; - [UI] private readonly Entry PlayerNameEntry = null; - [UI] private readonly Entry PlayerAliasEntry = null; - [UI] private readonly Entry PlayerHexIdEntry = null; - [UI] private readonly CheckButton PlayerAnonymousCheckButton = null; - [UI] private readonly Button CancelPlayerEditorButton = null; - [UI] private readonly Button SavePlayerButton = null; - - public event DialogClosedEventHandler OnDialogClosed; + [UI] private readonly Entry _nameEntry = null; + [UI] private readonly Entry _aliasEntry = null; + [UI] private readonly Entry _hexIdEntry = null; + [UI] private readonly CheckButton _anonCheckButton = null; - public PlayerEditor(Window parent, Player player) + public PlayerEditor(Window parent, Player player) : base(parent, new Builder("PlayerEditor.glade")) { - _player = player; - - var builder = new Builder("Dialogs.glade"); - builder.Autoconnect(this); - - SavePlayerButton.Clicked += SavePlayerButtonOnClicked; - CancelPlayerEditorButton.Clicked += (sender, args) => - { - OnDialogClosed?.Invoke(this, new DialogClosedEventArgs(false, null)); - PlayerEditorDialog.Dispose(); - }; - - PlayerEditorDialog.TransientFor = parent; - PlayerEditorDialog.Show(); - - ReadFromModel(); - } - - // Events - - private void SavePlayerButtonOnClicked(object sender, EventArgs e) - { - ReadToModel(); - OnDialogClosed?.Invoke(this, new DialogClosedEventArgs(true, _player)); - PlayerEditorDialog.Dispose(); - } - - // Private Methods - - private void ReadToModel() - { - _player.Name = PlayerNameEntry.Text; - _player.Alias = PlayerAliasEntry.Text; - _player.HexId = PlayerHexIdEntry.Text; - _player.Anonymous = PlayerAnonymousCheckButton.Active; + EditObject = player; } - private void ReadFromModel() + protected override void LoadToModel() { - PlayerNameEntry.Text = _player.Name; - PlayerAliasEntry.Text = _player.Alias; - PlayerHexIdEntry.Text = _player.HexId; - PlayerAnonymousCheckButton.Active = _player.Anonymous; + EditObject.Name = _nameEntry.Text; + EditObject.Alias = _aliasEntry.Text; + EditObject.HexId = _hexIdEntry.Text; + EditObject.Anonymous = _anonCheckButton.Active; + } + + protected override void LoadFromModel() + { + _nameEntry.Text = EditObject.Name; + _aliasEntry.Text = EditObject.Alias; + _hexIdEntry.Text = EditObject.HexId; + _anonCheckButton.Active = EditObject.Anonymous; } } } \ No newline at end of file diff --git a/EstusShots.Gtk/Pages/BaseDataPage.cs b/EstusShots.Gtk/Pages/BaseDataPage.cs index 45e79e4..d022b1b 100644 --- a/EstusShots.Gtk/Pages/BaseDataPage.cs +++ b/EstusShots.Gtk/Pages/BaseDataPage.cs @@ -44,6 +44,12 @@ namespace EstusShots.Gtk new DataColumnDouble(nameof(Drink.Vol)) {Title = "%"} }; _drinksControl = new BindableListControl(drinkColumns, nameof(Drink.DrinkId), DrinksTreeView); + _drinksControl.ItemActivated += item => + { + var drinkEditor = new DrinkEditor(this, item); + drinkEditor.DialogClosed += DrinkEditorClosed; + drinkEditor.Show(); + }; // TODO Only Load when navigated to Task _; @@ -51,32 +57,36 @@ namespace EstusShots.Gtk _ = ReloadDrinks(); } + + // Events private void PlayersControlActivated(Player player) { var dialog = new PlayerEditor(this, player); - dialog.OnDialogClosed += PlayerEditorClosed; + dialog.DialogClosed += PlayerEditorClosed; + dialog.Show(); } private void NewPlayerButtonOnClicked(object sender, EventArgs e) { var dialog = new PlayerEditor(this, new Player()); - dialog.OnDialogClosed += PlayerEditorClosed; + dialog.DialogClosed += PlayerEditorClosed; + dialog.Show(); } private void NewDrinkButtonOnClicked(object sender, EventArgs e) { var dialog = new DrinkEditor(this, new Drink()); - dialog.OnDialogClosed += DialogOnOnDialogClosed; + dialog.DialogClosed += DrinkEditorClosed; + dialog.Show(); } - private async void DialogOnOnDialogClosed(object o, DialogClosedEventArgs args) + private async void DrinkEditorClosed(object o, DialogClosedEventArgs args) { - if (!args.Ok || !(args.Model is Drink drink)) return; - + if (!args.Ok) return; var res = await Task.Factory.StartNew(() - => Client.Drinks.SaveDrink(new SaveDrinkParameter(drink)).Result); + => Client.Drinks.SaveDrink(new SaveDrinkParameter(args.Model)).Result); if (!res.OperationResult.Success) { @@ -88,11 +98,11 @@ namespace EstusShots.Gtk await ReloadDrinks(); } - private async void PlayerEditorClosed(object o, DialogClosedEventArgs args) + private async void PlayerEditorClosed(object o, DialogClosedEventArgs args) { - if (!args.Ok || !(args.Model is Player player)) return; + if (!args.Ok) return; var res = await Task.Factory.StartNew(() - => Client.Players.SavePlayer(new SavePlayerParameter(player)).Result); + => Client.Players.SavePlayer(new SavePlayerParameter(args.Model)).Result); if (!res.OperationResult.Success) { Info($"Unable to save: {res.OperationResult.ShortMessage}"); diff --git a/EstusShots.Gtk/Pages/SeasonsPage.cs b/EstusShots.Gtk/Pages/SeasonsPage.cs index bdfc720..1effad0 100644 --- a/EstusShots.Gtk/Pages/SeasonsPage.cs +++ b/EstusShots.Gtk/Pages/SeasonsPage.cs @@ -43,7 +43,7 @@ namespace EstusShots.Gtk private void NewSeasonButtonOnClicked(object sender, EventArgs e) { var dialog = new SeasonEditor(this, new Season()); - dialog.OnDialogClosed += SeasonEditorClosed; + dialog.DialogClosed += SeasonEditorClosed; dialog.Show(); } @@ -80,7 +80,7 @@ namespace EstusShots.Gtk private void SeasonsControlItemActivated(Season item) { var dialog = new SeasonEditor(this, item); - dialog.OnDialogClosed += SeasonEditorClosed; + dialog.DialogClosed += SeasonEditorClosed; dialog.Show(); }