From 3536b626d427282ba871cb249a3217d9a31512fa Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Fri, 18 Nov 2011 01:52:16 +0000 Subject: [PATCH] mac: move logic from setup to its controller, check events instead of timers --- SparkleShare/Mac/SparkleEventLog.cs | 2 +- SparkleShare/Mac/SparkleSetup.cs | 139 ++++++++++++++++--------- SparkleShare/SparkleSetupController.cs | 35 +++++++ 3 files changed, 128 insertions(+), 48 deletions(-) diff --git a/SparkleShare/Mac/SparkleEventLog.cs b/SparkleShare/Mac/SparkleEventLog.cs index 7cb344d7..17d7056c 100755 --- a/SparkleShare/Mac/SparkleEventLog.cs +++ b/SparkleShare/Mac/SparkleEventLog.cs @@ -193,7 +193,7 @@ namespace SparkleShare { public override void DecidePolicyForNavigation (WebView web_view, NSDictionary action_info, NSUrlRequest request, WebFrame frame, NSObject decision_token) { - Controller.LinkClicked (request.Url.ToString ()); + SparkleEventLogController.LinkClicked (request.Url.ToString ()); } } } diff --git a/SparkleShare/Mac/SparkleSetup.cs b/SparkleShare/Mac/SparkleSetup.cs index f03cc37b..68bd39b9 100755 --- a/SparkleShare/Mac/SparkleSetup.cs +++ b/SparkleShare/Mac/SparkleSetup.cs @@ -16,11 +16,9 @@ using System; +using System.Collections.Generic; using System.Drawing; using System.IO; -using System.Timers; - -using System.Collections.Generic; using Mono.Unix; using MonoMac.Foundation; @@ -54,7 +52,6 @@ namespace SparkleShare { private NSTextField PathLabel; private NSTextField PathHelpLabel; private NSTextField AddProjectTextField; - private Timer timer; private NSTableView TableView; private NSScrollView ScrollView; private NSTableColumn IconColumn; @@ -87,8 +84,9 @@ namespace SparkleShare { }; FullNameTextField = new NSTextField () { - Frame = new RectangleF (330, Frame.Height - 238, 196, 22), - StringValue = Controller.GuessedUserName + Frame = new RectangleF (330, Frame.Height - 238, 196, 22), + StringValue = Controller.GuessedUserName, + Delegate = new SparkleTextFieldDelegate () }; EmailLabel = new NSTextField () { @@ -102,42 +100,45 @@ namespace SparkleShare { }; EmailTextField = new NSTextField () { - Frame = new RectangleF (330, Frame.Height - 268, 196, 22), - StringValue = Controller.GuessedUserEmail + Frame = new RectangleF (330, Frame.Height - 268, 196, 22), + StringValue = Controller.GuessedUserEmail, + Delegate = new SparkleTextFieldDelegate () }; - // TODO: Ugly hack, do properly with events - timer = new Timer () { - Interval = 50 + + (FullNameTextField.Delegate as SparkleTextFieldDelegate).StringValueChanged += delegate { + Controller.CheckSetupPage ( + FullNameTextField.StringValue, + EmailTextField.StringValue + ); }; + (EmailTextField.Delegate as SparkleTextFieldDelegate).StringValueChanged += delegate { + Controller.CheckSetupPage ( + FullNameTextField.StringValue, + EmailTextField.StringValue + ); + }; + + ContinueButton = new NSButton () { Title = "Continue", Enabled = false }; ContinueButton.Activated += delegate { - timer.Stop (); - timer = null; - string full_name = FullNameTextField.StringValue.Trim (); string email = EmailTextField.StringValue.Trim (); Controller.SetupPageCompleted (full_name, email); }; - timer.Elapsed += delegate { + Controller.UpdateSetupContinueButtonEvent += delegate (bool button_enabled) { InvokeOnMainThread (delegate { - bool name_is_valid = !FullNameTextField.StringValue.Trim ().Equals (""); - - bool email_is_valid = Program.Controller.IsValidEmail ( - EmailTextField.StringValue.Trim ()); - - ContinueButton.Enabled = (name_is_valid && email_is_valid); + ContinueButton.Enabled = button_enabled; }); }; - timer.Start (); ContentView.AddSubview (FullNameLabel); ContentView.AddSubview (FullNameTextField); @@ -168,7 +169,8 @@ namespace SparkleShare { Frame = new RectangleF (190, Frame.Height - 336, 196, 22), Font = SparkleUI.Font, StringValue = Controller.PreviousAddress, - Enabled = (Controller.SelectedPlugin.Address == null) + Enabled = (Controller.SelectedPlugin.Address == null), + Delegate = new SparkleTextFieldDelegate () }; @@ -186,12 +188,12 @@ namespace SparkleShare { Frame = new RectangleF (190 + 196 + 16, Frame.Height - 336, 196, 22), StringValue = Controller.PreviousPath, Enabled = (Controller.SelectedPlugin.Path == null), - Bezeled = true + Delegate = new SparkleTextFieldDelegate () }; - AddressTextField.Cell.LineBreakMode = NSLineBreakMode.TruncatingTail; - PathTextField.Cell.LineBreakMode = NSLineBreakMode.TruncatingTail; + AddressTextField.Cell.LineBreakMode = NSLineBreakMode.TruncatingTail; + PathTextField.Cell.LineBreakMode = NSLineBreakMode.TruncatingTail; PathHelpLabel = new NSTextField () { @@ -210,7 +212,8 @@ namespace SparkleShare { Frame = new RectangleF (0, 0, 0, 0), RowHeight = 30, IntercellSpacing = new SizeF (0, 12), - HeaderView = null + HeaderView = null, + Delegate = new SparkleTableViewDelegate () }; ScrollView = new NSScrollView () { @@ -270,33 +273,43 @@ namespace SparkleShare { }); }; + TableView.SelectRow (Controller.SelectedPluginIndex, false); - timer = new Timer () { - Interval = 50 + (AddressTextField.Delegate as SparkleTextFieldDelegate).StringValueChanged += delegate { + Controller.CheckAddPage ( + AddressTextField.StringValue, + PathTextField.StringValue, + TableView.SelectedRow + ); }; - // TODO: Use an event - timer.Elapsed += delegate { - if (TableView.SelectedRow != Controller.SelectedPluginIndex) - Controller.SelectedPluginChanged (TableView.SelectedRow); + (PathTextField.Delegate as SparkleTextFieldDelegate).StringValueChanged += delegate { + Controller.CheckAddPage ( + AddressTextField.StringValue, + PathTextField.StringValue, + TableView.SelectedRow + ); + }; + (TableView.Delegate as SparkleTableViewDelegate).SelectionChanged += delegate { + Controller.SelectedPluginChanged (TableView.SelectedRow); + + Controller.CheckAddPage ( + AddressTextField.StringValue, + PathTextField.StringValue, + TableView.SelectedRow + ); + }; + + + Controller.UpdateAddProjectButtonEvent += delegate (bool button_enabled) { InvokeOnMainThread (delegate { - // TODO: Move checking logic to controller - if (!string.IsNullOrWhiteSpace (AddressTextField.StringValue) && - !string.IsNullOrWhiteSpace (PathTextField.StringValue)) { - - SyncButton.Enabled = true; - - } else { - SyncButton.Enabled = false; - } + SyncButton.Enabled = button_enabled; }); }; - timer.Start (); - ContentView.AddSubview (ScrollView); ContentView.AddSubview (AddressLabel); @@ -311,9 +324,6 @@ namespace SparkleShare { }; SyncButton.Activated += delegate { - timer.Stop (); - timer = null; - Controller.AddPageCompleted ( AddressTextField.StringValue, PathTextField.StringValue @@ -673,4 +683,39 @@ namespace SparkleShare { } } } + + + public class SparkleTextFieldDelegate : NSTextFieldDelegate { + + public event StringValueChangedHandler StringValueChanged; + public delegate void StringValueChangedHandler (); + + + public override void Changed (NSNotification notification) + { + if (StringValueChanged!= null) + StringValueChanged (); + } + + + public override string [] GetCompletions (NSControl control, NSTextView text_view, + string [] a, MonoMac.Foundation.NSRange range, int b) + { + return new string [0]; + } + } + + + public class SparkleTableViewDelegate : NSTableViewDelegate { + + public event SelectionChangedHandler SelectionChanged; + public delegate void SelectionChangedHandler (); + + + public override void SelectionDidChange (NSNotification notification) + { + if (SelectionChanged != null) + SelectionChanged (); + } + } } diff --git a/SparkleShare/SparkleSetupController.cs b/SparkleShare/SparkleSetupController.cs index 317a7c27..6bbc0693 100755 --- a/SparkleShare/SparkleSetupController.cs +++ b/SparkleShare/SparkleSetupController.cs @@ -41,6 +41,12 @@ namespace SparkleShare { public event UpdateProgressBarEventHandler UpdateProgressBarEvent; public delegate void UpdateProgressBarEventHandler (double percentage); + public event UpdateSetupContinueButtonEventHandler UpdateSetupContinueButtonEvent; + public delegate void UpdateSetupContinueButtonEventHandler (bool button_enabled); + + public event UpdateAddProjectButtonEventHandler UpdateAddProjectButtonEvent; + public delegate void UpdateAddProjectButtonEventHandler (bool button_enabled); + public event ChangeAddressFieldEventHandler ChangeAddressFieldEvent; public delegate void ChangeAddressFieldEventHandler (string text, string example_text, FieldState state); @@ -153,6 +159,19 @@ namespace SparkleShare { } + public void CheckSetupPage (string full_name, string email) + { + full_name = full_name.Trim (); + email = email.Trim (); + + bool fields_valid = (!string.IsNullOrWhiteSpace (full_name) && + Program.Controller.IsValidEmail (email)); + + if (UpdateSetupContinueButtonEvent != null) + UpdateSetupContinueButtonEvent (fields_valid); + } + + public void SetupPageCompleted (string full_name, string email) { Program.Controller.UserName = full_name; @@ -193,6 +212,22 @@ namespace SparkleShare { } + public void CheckAddPage (string address, string remote_path, int selected_plugin) + { + if (SelectedPluginIndex != selected_plugin) + SelectedPluginChanged (selected_plugin); + + address = address.Trim (); + remote_path = remote_path.Trim (); + + bool fields_valid = (!string.IsNullOrWhiteSpace (address) && + !string.IsNullOrWhiteSpace (remote_path)); + + if (UpdateAddProjectButtonEvent != null) + UpdateAddProjectButtonEvent (fields_valid); + } + + public void AddPageCompleted (string address, string path) { this.syncing_folder = Path.GetFileNameWithoutExtension (path);