summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/window_size_autosaver.mm
blob: 5ca987836a8b06f2413c450d736d89a0d055e1cb (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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#import "chrome/browser/ui/cocoa/window_size_autosaver.h"

#include "chrome/browser/prefs/pref_service.h"

// If the window width stored in the prefs is smaller than this, the size is
// not restored but instead cleared from the profile -- to protect users from
// accidentally making their windows very small and then not finding them again.
const int kMinWindowWidth = 101;

// Minimum restored window height, see |kMinWindowWidth|.
const int kMinWindowHeight = 17;

@interface WindowSizeAutosaver (Private)
- (void)save:(NSNotification*)notification;
- (void)restore;
@end

@implementation WindowSizeAutosaver

- (id)initWithWindow:(NSWindow*)window
         prefService:(PrefService*)prefs
                path:(const char*)path {
  if ((self = [super init])) {
    window_ = window;
    prefService_ = prefs;
    path_ = path;

    [self restore];
    [[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(save:)
             name:NSWindowDidMoveNotification
           object:window_];
    [[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(save:)
             name:NSWindowDidResizeNotification
           object:window_];
  }
  return self;
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [super dealloc];
}

- (void)save:(NSNotification*)notification {
  DictionaryValue* windowPrefs = prefService_->GetMutableDictionary(path_);
  NSRect frame = [window_ frame];
  if ([window_ styleMask] & NSResizableWindowMask) {
    // Save the origin of the window.
    windowPrefs->SetInteger("left", NSMinX(frame));
    windowPrefs->SetInteger("right", NSMaxX(frame));
    // windows's and linux's profiles have top < bottom due to having their
    // screen origin in the upper left, while cocoa's is in the lower left. To
    // keep the top < bottom invariant, store top in bottom and vice versa.
    windowPrefs->SetInteger("top", NSMinY(frame));
    windowPrefs->SetInteger("bottom", NSMaxY(frame));
  } else {
    // Save the origin of the window.
    windowPrefs->SetInteger("x", frame.origin.x);
    windowPrefs->SetInteger("y", frame.origin.y);
  }
}

- (void)restore {
  // Get the positioning information.
  DictionaryValue* windowPrefs = prefService_->GetMutableDictionary(path_);
  if ([window_ styleMask] & NSResizableWindowMask) {
    int x1, x2, y1, y2;
    if (!windowPrefs->GetInteger("left", &x1) ||
        !windowPrefs->GetInteger("right", &x2) ||
        !windowPrefs->GetInteger("top", &y1) ||
        !windowPrefs->GetInteger("bottom", &y2)) {
      return;
    }
    if (x2 - x1 < kMinWindowWidth || y2 - y1 < kMinWindowHeight) {
      // Windows should never be very small.
      windowPrefs->Remove("left", NULL);
      windowPrefs->Remove("right", NULL);
      windowPrefs->Remove("top", NULL);
      windowPrefs->Remove("bottom", NULL);
    } else {
      [window_ setFrame:NSMakeRect(x1, y1, x2 - x1, y2 - y1) display:YES];

      // Make sure the window is on-screen.
      [window_ cascadeTopLeftFromPoint:NSZeroPoint];
    }
  } else {
    int x, y;
    if (!windowPrefs->GetInteger("x", &x) ||
        !windowPrefs->GetInteger("y", &y))
       return;  // Nothing stored.
    // Turn the origin (lower-left) into an upper-left window point.
    NSPoint upperLeft = NSMakePoint(x, y + NSHeight([window_ frame]));
    [window_ cascadeTopLeftFromPoint:upperLeft];
  }
}

@end