OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <Cocoa/Cocoa.h> |
| 6 |
| 7 #import "chrome/browser/cocoa/window_size_autosaver.h" |
| 8 |
| 9 #include "chrome/common/pref_service.h" |
| 10 |
| 11 @interface WindowSizeAutosaver (Private) |
| 12 - (void)save:(NSNotification*)notification; |
| 13 - (void)restore; |
| 14 @end |
| 15 |
| 16 @implementation WindowSizeAutosaver |
| 17 |
| 18 - (id)initWithWindow:(NSWindow*)window |
| 19 prefService:(PrefService*)prefs |
| 20 path:(const wchar_t*)path |
| 21 state:(WindowSizeAutosaverState)state { |
| 22 if ((self = [super init])) { |
| 23 window_ = window; |
| 24 prefService_ = prefs; |
| 25 path_ = path; |
| 26 state_ = state; |
| 27 |
| 28 [self restore]; |
| 29 [[NSNotificationCenter defaultCenter] |
| 30 addObserver:self |
| 31 selector:@selector(save:) |
| 32 name:NSWindowDidMoveNotification |
| 33 object:window_]; |
| 34 [[NSNotificationCenter defaultCenter] |
| 35 addObserver:self |
| 36 selector:@selector(save:) |
| 37 name:NSWindowDidResizeNotification |
| 38 object:window_]; |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 - (void)dealloc { |
| 44 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 45 [super dealloc]; |
| 46 } |
| 47 |
| 48 - (void)save:(NSNotification*)notification { |
| 49 DictionaryValue* windowPrefs = prefService_->GetMutableDictionary(path_); |
| 50 NSRect frame = [window_ frame]; |
| 51 if (state_ == kSaveWindowRect) { |
| 52 // Save the origin of the window. |
| 53 windowPrefs->SetInteger(L"left", NSMinX(frame)); |
| 54 windowPrefs->SetInteger(L"right", NSMaxX(frame)); |
| 55 // windows's and linux's profiles have top < bottom due to having their |
| 56 // screen origin in the upper left, while cocoa's is in the lower left. To |
| 57 // keep the top < bottom invariant, store top in bottom and vice versa. |
| 58 windowPrefs->SetInteger(L"top", NSMinY(frame)); |
| 59 windowPrefs->SetInteger(L"bottom", NSMaxY(frame)); |
| 60 } else if (state_ == kSaveWindowPos) { |
| 61 // Save the origin of the window. |
| 62 windowPrefs->SetInteger(L"x", frame.origin.x); |
| 63 windowPrefs->SetInteger(L"y", frame.origin.y); |
| 64 } else { |
| 65 NOTREACHED(); |
| 66 } |
| 67 } |
| 68 |
| 69 - (void)restore { |
| 70 // Get the positioning information. |
| 71 DictionaryValue* windowPrefs = prefService_->GetMutableDictionary(path_); |
| 72 if (state_ == kSaveWindowRect) { |
| 73 int x1, x2, y1, y2; |
| 74 if (!windowPrefs->GetInteger(L"left", &x1) || |
| 75 !windowPrefs->GetInteger(L"right", &x2) || |
| 76 !windowPrefs->GetInteger(L"top", &y1) || |
| 77 !windowPrefs->GetInteger(L"bottom", &y2)) { |
| 78 return; |
| 79 } |
| 80 [window_ setFrame:NSMakeRect(x1, y1, x2 - x1, y2 - y1) display:YES]; |
| 81 } else if (state_ == kSaveWindowPos) { |
| 82 int x, y; |
| 83 if (!windowPrefs->GetInteger(L"x", &x) || |
| 84 !windowPrefs->GetInteger(L"y", &y)) |
| 85 return; // Nothing stored. |
| 86 // Turn the origin (lower-left) into an upper-left window point. |
| 87 NSPoint upperLeft = NSMakePoint(x, y + NSHeight([window_ frame])); |
| 88 [window_ cascadeTopLeftFromPoint:upperLeft]; |
| 89 } else { |
| 90 NOTREACHED(); |
| 91 } |
| 92 } |
| 93 |
| 94 @end |
| 95 |
OLD | NEW |