summaryrefslogtreecommitdiff
path: root/SparkleShare/Mac/UserInterface/Note.cs
blob: 9e5c2472592ff2ec7a06632d4ca2b72a15e1b70b (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
220
221
222
223
224
225
226
227
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hi@planetpeanut.uk>
//
//   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 AppKit;
using CoreGraphics;
using Foundation;

namespace SparkleShare {

    public class Note : NSWindow {

        public NoteController Controller = new NoteController ();

        private NSImage user_image, balloon_image;
        private NSImageView user_image_view, balloon_image_view;
        private NSButton hidden_close_button, cancel_button, sync_button;
        private NSBox cover;
        private NSTextField user_name_text_field, user_email_text_field, balloon_text_field;


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

        public Note () : base ()
        {
            SetFrame (new CGRect (0, 0, 480, 240), true);
            Center ();

            Delegate    = new SparkleNoteDelegate ();
            StyleMask   = (NSWindowStyle.Closable | NSWindowStyle.Titled);
            Title       = "Add Note";
            MaxSize     = new CGSize (480, 240);
            MinSize     = new CGSize (480, 240);
            HasShadow   = true;
            IsOpaque    = false;
            BackingType = NSBackingStore.Buffered;
            Level       = NSWindowLevel.Floating;

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

            CreateNote ();


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

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

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

            Controller.UpdateTitleEvent += delegate (string title) {
                SparkleShare.Controller.Invoke (() => { Title = title; });
            };


            ContentView.AddSubview (this.hidden_close_button);
        }


        private void CreateNote ()
        {
            this.cover = new NSBox () {
                Frame = new CGRect (
                    new CGPoint (-1, 58),
                    new CGSize (Frame.Width + 2, this.ContentView.Frame.Height + 1)),
                FillColor = NSColor.FromCalibratedRgba (0.77f, 0.77f, 0.75f, 1.0f),
                BorderColor = NSColor.LightGray,
                BoxType = NSBoxType.NSBoxCustom
            };


            this.user_name_text_field = new NSTextField () {
                Alignment       = NSTextAlignment.Left,
                BackgroundColor = NSColor.FromCalibratedRgba (0.77f, 0.77f, 0.75f, 1.0f),
                Bordered        = false,
                Editable        = false,
                Frame           = new CGRect (
                    new CGPoint (85, ContentView.Frame.Height - 41),
                    new CGSize (320, 22)),
                StringValue     = SparkleShare.Controller.CurrentUser.Name,
                Font            = NSFont.BoldSystemFontOfSize (12)
            };
            
            this.user_email_text_field = new NSTextField () {
                Alignment       = NSTextAlignment.Left,
                BackgroundColor = NSColor.FromCalibratedRgba (0.77f, 0.77f, 0.75f, 1.0f),
                TextColor       = NSColor.DisabledControlText,
                Bordered        = false,
                Editable        = false,
                Frame           = new CGRect (
                    new CGPoint (85, ContentView.Frame.Height - 60),
                    new CGSize (320, 20)),
                StringValue     = SparkleShare.Controller.CurrentUser.Email,
            };

            
            this.balloon_text_field = new NSTextField () {
                Alignment       = NSTextAlignment.Left,
                BackgroundColor = NSColor.White,
                Bordered        = false,
                Editable        = true,
                Frame           = new CGRect (
                    new CGPoint (30, ContentView.Frame.Height - 137),
                    new CGSize (418, 48))
            };

            (this.balloon_text_field.Cell as NSTextFieldCell).PlaceholderString  = "Anything to add?";
            (this.balloon_text_field.Cell as NSTextFieldCell).LineBreakMode      = NSLineBreakMode.ByWordWrapping;
            (this.balloon_text_field.Cell as NSTextFieldCell).UsesSingleLineMode = false;

            this.balloon_text_field.Cell.FocusRingType = NSFocusRingType.None;

            
            this.cancel_button = new NSButton () {
                Title = "Cancel",
                BezelStyle = NSBezelStyle.Rounded,
                Frame      = new CGRect (Frame.Width - 15 - 105 * 2, 12, 105, 32),
            };

            this.sync_button = new NSButton () {
                Title = "Sync",
                BezelStyle = NSBezelStyle.Rounded,
                Frame      = new CGRect (Frame.Width - 15 - 105, 12, 105, 32),
            };

            this.cancel_button.Activated += delegate { Controller.CancelClicked (); };
            this.sync_button.Activated += delegate { Controller.SyncClicked (this.balloon_text_field.StringValue); };

            DefaultButtonCell = this.sync_button.Cell;


            if (BackingScaleFactor >= 2)
                this.balloon_image = NSImage.ImageNamed ("text-balloon@2x");
            else
                this.balloon_image = NSImage.ImageNamed ("text-balloon");

            this.balloon_image.Size = new CGSize (438, 72);
            this.balloon_image_view = new NSImageView () { 
                Image = this.balloon_image,
                Frame = new CGRect (21, ContentView.Frame.Height - 145, 438, 72)
            };


            if (!string.IsNullOrEmpty (Controller.AvatarFilePath))
                this.user_image = new NSImage (Controller.AvatarFilePath);
            else
                this.user_image = NSImage.ImageNamed ("user-icon-default");

            this.user_image.Size = new CGSize (48, 48);
            this.user_image_view = new NSImageView () {
                Image = this.user_image,
                Frame = new CGRect (21, ContentView.Frame.Height - 65, 48, 48)
            };

            this.user_image_view.WantsLayer          = true;
            this.user_image_view.Layer.CornerRadius  = 5.0f;
            this.user_image_view.Layer.MasksToBounds = true;


            ContentView.AddSubview (this.cover);
            ContentView.AddSubview (this.cancel_button);
            ContentView.AddSubview (this.sync_button);
            ContentView.AddSubview (this.user_name_text_field);
            ContentView.AddSubview (this.user_email_text_field);

            ContentView.AddSubview (this.user_image_view);
            ContentView.AddSubview (this.balloon_image_view);
            ContentView.AddSubview (this.balloon_text_field);

            MakeFirstResponder ((NSResponder) this.balloon_text_field);
        }


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


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


        public override bool AcceptsFirstResponder ()
        {
            return true;
        }


        class SparkleNoteDelegate : NSWindowDelegate {
            
            public override bool WindowShouldClose (NSObject sender)
            {
                (sender as Note).Controller.WindowClosed ();
                return false;
            }
        }
    }
}