Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #import "chrome/browser/ui/cocoa/window_size_autosaver.h" | 7 #import "chrome/browser/ui/cocoa/window_size_autosaver.h" |
| 8 | 8 |
| 9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
| 10 | 11 |
| 11 // If the window width stored in the prefs is smaller than this, the size is | 12 // If the window width stored in the prefs is smaller than this, the size is |
| 12 // not restored but instead cleared from the profile -- to protect users from | 13 // not restored but instead cleared from the profile -- to protect users from |
| 13 // accidentally making their windows very small and then not finding them again. | 14 // accidentally making their windows very small and then not finding them again. |
| 14 const int kMinWindowWidth = 101; | 15 const int kMinWindowWidth = 101; |
| 15 | 16 |
| 16 // Minimum restored window height, see |kMinWindowWidth|. | 17 // Minimum restored window height, see |kMinWindowWidth|. |
| 17 const int kMinWindowHeight = 17; | 18 const int kMinWindowHeight = 17; |
| 18 | 19 |
| 19 @interface WindowSizeAutosaver (Private) | 20 @interface WindowSizeAutosaver (Private) |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 45 } | 46 } |
| 46 return self; | 47 return self; |
| 47 } | 48 } |
| 48 | 49 |
| 49 - (void)dealloc { | 50 - (void)dealloc { |
| 50 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 51 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 51 [super dealloc]; | 52 [super dealloc]; |
| 52 } | 53 } |
| 53 | 54 |
| 54 - (void)save:(NSNotification*)notification { | 55 - (void)save:(NSNotification*)notification { |
| 55 DictionaryValue* windowPrefs = prefService_->GetMutableDictionary(path_); | 56 DictionaryPrefUpdate update(prefService_, path_); |
| 57 DictionaryValue* windowPrefs = update.Get(); | |
| 56 NSRect frame = [window_ frame]; | 58 NSRect frame = [window_ frame]; |
| 57 if ([window_ styleMask] & NSResizableWindowMask) { | 59 if ([window_ styleMask] & NSResizableWindowMask) { |
| 58 // Save the origin of the window. | 60 // Save the origin of the window. |
| 59 windowPrefs->SetInteger("left", NSMinX(frame)); | 61 windowPrefs->SetInteger("left", NSMinX(frame)); |
| 60 windowPrefs->SetInteger("right", NSMaxX(frame)); | 62 windowPrefs->SetInteger("right", NSMaxX(frame)); |
| 61 // windows's and linux's profiles have top < bottom due to having their | 63 // windows's and linux's profiles have top < bottom due to having their |
| 62 // screen origin in the upper left, while cocoa's is in the lower left. To | 64 // screen origin in the upper left, while cocoa's is in the lower left. To |
| 63 // keep the top < bottom invariant, store top in bottom and vice versa. | 65 // keep the top < bottom invariant, store top in bottom and vice versa. |
| 64 windowPrefs->SetInteger("top", NSMinY(frame)); | 66 windowPrefs->SetInteger("top", NSMinY(frame)); |
| 65 windowPrefs->SetInteger("bottom", NSMaxY(frame)); | 67 windowPrefs->SetInteger("bottom", NSMaxY(frame)); |
| 66 } else { | 68 } else { |
| 67 // Save the origin of the window. | 69 // Save the origin of the window. |
| 68 windowPrefs->SetInteger("x", frame.origin.x); | 70 windowPrefs->SetInteger("x", frame.origin.x); |
| 69 windowPrefs->SetInteger("y", frame.origin.y); | 71 windowPrefs->SetInteger("y", frame.origin.y); |
| 70 } | 72 } |
| 71 } | 73 } |
| 72 | 74 |
| 73 - (void)restore { | 75 - (void)restore { |
| 74 // Get the positioning information. | 76 // Get the positioning information. |
| 75 DictionaryValue* windowPrefs = prefService_->GetMutableDictionary(path_); | 77 DictionaryPrefUpdate update(prefService_, path_); |
| 78 DictionaryValue* windowPrefs = update.Get(); | |
| 76 if ([window_ styleMask] & NSResizableWindowMask) { | 79 if ([window_ styleMask] & NSResizableWindowMask) { |
| 77 int x1, x2, y1, y2; | 80 int x1, x2, y1, y2; |
| 78 if (!windowPrefs->GetInteger("left", &x1) || | 81 if (!windowPrefs->GetInteger("left", &x1) || |
| 79 !windowPrefs->GetInteger("right", &x2) || | 82 !windowPrefs->GetInteger("right", &x2) || |
| 80 !windowPrefs->GetInteger("top", &y1) || | 83 !windowPrefs->GetInteger("top", &y1) || |
| 81 !windowPrefs->GetInteger("bottom", &y2)) { | 84 !windowPrefs->GetInteger("bottom", &y2)) { |
| 82 return; | 85 return; |
| 83 } | 86 } |
| 84 if (x2 - x1 < kMinWindowWidth || y2 - y1 < kMinWindowHeight) { | 87 if (x2 - x1 < kMinWindowWidth || y2 - y1 < kMinWindowHeight) { |
| 85 // Windows should never be very small. | 88 // Windows should never be very small. |
|
viettrungluu
2011/04/04 20:44:09
Maybe the DictionaryPrefUpdate should only be used
| |
| 86 windowPrefs->Remove("left", NULL); | 89 windowPrefs->Remove("left", NULL); |
| 87 windowPrefs->Remove("right", NULL); | 90 windowPrefs->Remove("right", NULL); |
| 88 windowPrefs->Remove("top", NULL); | 91 windowPrefs->Remove("top", NULL); |
| 89 windowPrefs->Remove("bottom", NULL); | 92 windowPrefs->Remove("bottom", NULL); |
| 90 } else { | 93 } else { |
| 91 [window_ setFrame:NSMakeRect(x1, y1, x2 - x1, y2 - y1) display:YES]; | 94 [window_ setFrame:NSMakeRect(x1, y1, x2 - x1, y2 - y1) display:YES]; |
| 92 | 95 |
| 93 // Make sure the window is on-screen. | 96 // Make sure the window is on-screen. |
| 94 [window_ cascadeTopLeftFromPoint:NSZeroPoint]; | 97 [window_ cascadeTopLeftFromPoint:NSZeroPoint]; |
| 95 } | 98 } |
| 96 } else { | 99 } else { |
| 97 int x, y; | 100 int x, y; |
| 98 if (!windowPrefs->GetInteger("x", &x) || | 101 if (!windowPrefs->GetInteger("x", &x) || |
| 99 !windowPrefs->GetInteger("y", &y)) | 102 !windowPrefs->GetInteger("y", &y)) |
| 100 return; // Nothing stored. | 103 return; // Nothing stored. |
| 101 // Turn the origin (lower-left) into an upper-left window point. | 104 // Turn the origin (lower-left) into an upper-left window point. |
| 102 NSPoint upperLeft = NSMakePoint(x, y + NSHeight([window_ frame])); | 105 NSPoint upperLeft = NSMakePoint(x, y + NSHeight([window_ frame])); |
| 103 [window_ cascadeTopLeftFromPoint:upperLeft]; | 106 [window_ cascadeTopLeftFromPoint:upperLeft]; |
| 104 } | 107 } |
| 105 } | 108 } |
| 106 | 109 |
| 107 @end | 110 @end |
| 108 | 111 |
| OLD | NEW |