summaryrefslogtreecommitdiff
path: root/SparkleShare/Mac/SparkleAbout.cs
blob: 448f7a1838fbc4c9c75200b21959f2650ad627e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hylkebons@gmail.com>
//
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Drawing;
using System.IO;

using MonoMac.AppKit;
using MonoMac.Foundation;

namespace SparkleShare {

    public class SparkleAbout : NSWindow {

        public SparkleAboutController Controller = new SparkleAboutController ();

        private NSTextField version_text_field, updates_text_field, credits_text_field;
        private SparkleLink website_link, credits_link, report_problem_link, debug_log_link;
        private NSImage about_image;
        private NSImageView about_image_view;
        private NSButton hidden_close_button;


        public SparkleAbout (IntPtr handle) : base (handle) { }

        public SparkleAbout () : base ()
        {
            SetFrame (new RectangleF (0, 0, 640, 281), true);
            Center ();

            Delegate    = new SparkleAboutDelegate ();
            StyleMask   = (NSWindowStyle.Closable | NSWindowStyle.Titled);
            Title       = "About SparkleShare";
            MaxSize     = new SizeF (640, 281);
            MinSize     = new SizeF (640, 281);
            HasShadow   = true;
            BackingType = NSBackingStore.Buffered;
            Level       = NSWindowLevel.Floating;

            this.hidden_close_button = new NSButton () {
                Frame                     = new RectangleF (0, 0, 0, 0),
                KeyEquivalentModifierMask = NSEventModifierMask.CommandKeyMask,
                KeyEquivalent             = "w"
            };

            CreateAbout ();


            this.hidden_close_button.Activated += delegate { Controller.WindowClosed (); };

            Controller.HideWindowEvent += delegate {
                Program.Controller.Invoke (() => PerformClose (this));
            };

            Controller.ShowWindowEvent += delegate {
                Program.Controller.Invoke (() => OrderFrontRegardless ());
            };

            Controller.UpdateLabelEvent += delegate (string text) {
                Program.Controller.Invoke (() => { this.updates_text_field.StringValue = text; });
            };


            ContentView.AddSubview (this.hidden_close_button);
        }


        private void CreateAbout ()
        {
            this.about_image = NSImage.ImageNamed ("about");
            this.about_image.Size = new SizeF (720, 260);

            this.about_image_view = new NSImageView () {
                Image = this.about_image,
                Frame = new RectangleF (0, 0, 720, 260)
            };

            this.version_text_field = new SparkleLabel ("version " + Controller.RunningVersion, NSTextAlignment.Left) {
                DrawsBackground = false,
                Frame           = new RectangleF (295, 140, 318, 22),
                TextColor       = NSColor.White
            };

            this.updates_text_field = new SparkleLabel ("Checking for updates...", NSTextAlignment.Left) {
                DrawsBackground = false,
                Frame           = new RectangleF (295, Frame.Height - 232, 318, 98),
                TextColor       = NSColor.FromCalibratedRgba (1.0f, 1.0f, 1.0f, 0.5f)
            };

            this.credits_text_field = new SparkleLabel (
                @"Copyright © 2010–" + DateTime.Now.Year + " Hylke Bons and others." +
                "\n\n" + 
                "SparkleShare is Open Source. You are free to use, modify, and redistribute it " +
                "under the GNU GPLv3.", NSTextAlignment.Left) {
                
                DrawsBackground = false,
                Frame           = new RectangleF (295, Frame.Height - 260, 318, 98),
                TextColor       = NSColor.White
            };

            this.website_link       = new SparkleLink ("Website", Controller.WebsiteLinkAddress);
            this.website_link.Frame = new RectangleF (new PointF (295, 25), this.website_link.Frame.Size);
            
            this.credits_link       = new SparkleLink ("Credits", Controller.CreditsLinkAddress);
            this.credits_link.Frame = new RectangleF (
                new PointF (this.website_link.Frame.X + this.website_link.Frame.Width + 10, 25),
                this.credits_link.Frame.Size);
            
            this.report_problem_link       = new SparkleLink ("Report a problem", Controller.ReportProblemLinkAddress);
            this.report_problem_link.Frame = new RectangleF (
                new PointF (this.credits_link.Frame.X + this.credits_link.Frame.Width + 10, 25),
                this.report_problem_link.Frame.Size);
            
            this.debug_log_link       = new SparkleLink ("Debug log", Controller.DebugLogLinkAddress);
            this.debug_log_link.Frame = new RectangleF (
                new PointF (this.report_problem_link.Frame.X + this.report_problem_link.Frame.Width + 10, 25),
                this.debug_log_link.Frame.Size);

            ContentView.AddSubview (this.about_image_view);
            ContentView.AddSubview (this.version_text_field);
            ContentView.AddSubview (this.updates_text_field);
            ContentView.AddSubview (this.credits_text_field);
            ContentView.AddSubview (this.website_link);
            ContentView.AddSubview (this.credits_link);
            ContentView.AddSubview (this.report_problem_link);
            ContentView.AddSubview (this.debug_log_link);
        }


        public override void OrderFrontRegardless ()
        {
            NSApplication.SharedApplication.ActivateIgnoringOtherApps (true);
            MakeKeyAndOrderFront (this);

            if (Program.UI != null)
                Program.UI.UpdateDockIconVisibility ();

            base.OrderFrontRegardless ();
        }


        public override void PerformClose (NSObject sender)
        {
            base.OrderOut (this);

            if (Program.UI != null)
                Program.UI.UpdateDockIconVisibility ();

            return;
        }


        private class SparkleAboutDelegate : NSWindowDelegate {
            
            public override bool WindowShouldClose (NSObject sender)
            {
                (sender as SparkleAbout).Controller.WindowClosed ();
                return false;
            }
        }
        
        
        private class SparkleLink : NSTextField {
            
            private NSUrl url;
            
            
            public SparkleLink (string text, string address) : base ()
            {
                this.url = new NSUrl (address);
                
                AllowsEditingTextAttributes = true;
                BackgroundColor = NSColor.White;
                Bordered        = false;
                DrawsBackground = false;
                Editable        = false;
                Selectable      = false;
                
                NSData name_data = NSData.FromString ("<a href='" + this.url +
                    "' style='font-size: 9pt; font-family: \"Helvetica Neue\"; color: #739ECF'>" + text + "</a></font>");
                
                NSDictionary name_dictionary       = new NSDictionary();
                NSAttributedString name_attributes = new NSAttributedString (name_data, new NSUrl ("file://"), out name_dictionary);
                
                NSMutableAttributedString s = new NSMutableAttributedString ();
                s.Append (name_attributes);
                
                Cell.AttributedStringValue = s;
                SizeToFit ();
            }
            
            
            public override void MouseUp (NSEvent e)
            {
                Program.Controller.OpenWebsite (this.url.ToString ());
            }
            
            
            public override void ResetCursorRects ()
            {
                AddCursorRect (Bounds, NSCursor.PointingHandCursor);
            }
        }
    }
}