2
// MonoMac.CFNetwork.Test.MainWindowController
5
// Martin Baulig (martin.baulig@gmail.com)
7
// Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
10
// Permission is hereby granted, free of charge, to any person obtaining
11
// a copy of this software and associated documentation files (the
12
// "Software"), to deal in the Software without restriction, including
13
// without limitation the rights to use, copy, modify, merge, publish,
14
// distribute, sublicense, and/or sell copies of the Software, and to
15
// permit persons to whom the Software is furnished to do so, subject to
16
// the following conditions:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
using System.Threading;
31
using System.Threading.Tasks;
32
using System.Collections.Generic;
34
using MonoMac.Foundation;
37
namespace MonoMac.CFNetwork.Test {
42
public partial class MainWindowController : MonoMac.AppKit.NSWindowController {
45
// Called when created from unmanaged code
46
public MainWindowController (IntPtr handle) : base (handle)
51
// Called when created directly from a XIB file
52
[Export ("initWithCoder:")]
53
public MainWindowController (NSCoder coder) : base (coder)
58
// Call to load from the XIB/NIB file
59
public MainWindowController () : base ("MainWindow")
64
// Shared initialization code
67
var runner = AsyncTaskRunnerController.Instance;
68
runner.StateChangedEvent += (sender, e) => {
69
IsRunning = e.IsRunning;
74
ShowProgress = runner.CanReportProgress;
76
runner.ProgressChangedEvent += (sender, e) => {
78
CurrentProgress = 100.0 * e.Current / e.Total.Value;
80
runner.MessageEvent += (sender, e) => {
81
StatusLabel.StringValue = e.Message;
84
getStringController = new GetStringViewController ();
86
checkHeadersViewController = new CheckHeadersViewController ();
87
downloadDataViewController = new DownloadDataViewController ();
88
benchmarkViewController = new BenchmarkViewController ();
93
//strongly typed window accessor
94
public new MainWindow Window {
96
return (MainWindow)base.Window;
100
public bool AutoRedirect {
105
public override void AwakeFromNib ()
107
base.AwakeFromNib ();
109
urlController = new URLViewController ();
110
urlController.View.AutoresizingMask = NSViewResizingMask.WidthSizable;
111
urlController.View.Frame = URLView.Bounds;
112
URLView.AddSubview (urlController.View);
115
URLViewController urlController;
116
GetStringViewController getStringController;
117
DownloadDataViewController downloadDataViewController;
118
CheckHeadersViewController checkHeadersViewController;
119
BenchmarkViewController benchmarkViewController;
120
NSViewController currentController;
122
internal CheckHeadersViewController HeaderController {
123
get { return checkHeadersViewController; }
126
internal BenchmarkViewController BenchmarkController {
127
get { return benchmarkViewController; }
130
public void DownloadData ()
132
SwitchView (downloadDataViewController, "Download Data");
135
public void GetString ()
137
SwitchView (getStringController, "Get String");
140
public void CheckHeaders ()
142
SwitchView (checkHeadersViewController, "Check Headers");
145
public void Benchmark ()
147
SwitchView (benchmarkViewController, "Benchmark");
150
void SwitchView (NSViewController controller, string name)
152
if (currentController != null)
153
currentController.View.RemoveFromSuperview ();
155
currentController = controller;
156
AsyncTaskRunnerController.Switch (((IAsyncViewController)controller).TaskRunner);
157
View.AddSubview (controller.View);
158
currentController.View.Frame = View.Bounds;
159
currentController.View.AutoresizingMask = NSViewResizingMask.HeightSizable |
160
NSViewResizingMask.WidthSizable;
167
double currentProgress;
169
[Export("IsRunning")]
170
public bool IsRunning {
171
get { return isRunning; }
173
WillChangeValue ("IsRunning");
175
View.NeedsDisplay = true;
176
DidChangeValue ("IsRunning");
180
[Export("ShowProgress")]
181
public bool ShowProgress {
182
get { return showProgress; }
184
WillChangeValue ("ShowProgress");
185
showProgress = value;
186
DidChangeValue ("ShowProgress");
190
[Export("CurrentProgress")]
191
public double CurrentProgress {
192
get { return currentProgress; }
194
WillChangeValue ("CurrentProgress");
195
currentProgress = value;
196
View.NeedsDisplay = true;
197
DidChangeValue ("CurrentProgress");
203
public void Load (string url)
205
AsyncTaskRunnerController.Instance.Run (new Uri (url));
210
AsyncTaskRunnerController.Instance.Cancel ();