Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.toolbar; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.PrefsHelper; |
michael@0 | 9 | import org.mozilla.gecko.Tab; |
michael@0 | 10 | import org.mozilla.gecko.Tabs; |
michael@0 | 11 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 12 | |
michael@0 | 13 | class ToolbarTitlePrefs { |
michael@0 | 14 | static final String PREF_TITLEBAR_MODE = "browser.chrome.titlebarMode"; |
michael@0 | 15 | static final String PREF_TRIM_URLS = "browser.urlbar.trimURLs"; |
michael@0 | 16 | |
michael@0 | 17 | interface OnChangeListener { |
michael@0 | 18 | public void onChange(); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | final String[] prefs = { |
michael@0 | 22 | PREF_TITLEBAR_MODE, |
michael@0 | 23 | PREF_TRIM_URLS |
michael@0 | 24 | }; |
michael@0 | 25 | |
michael@0 | 26 | private boolean mShowUrl; |
michael@0 | 27 | private boolean mTrimUrls; |
michael@0 | 28 | |
michael@0 | 29 | private Integer mPrefObserverId; |
michael@0 | 30 | |
michael@0 | 31 | ToolbarTitlePrefs() { |
michael@0 | 32 | mShowUrl = false; |
michael@0 | 33 | mTrimUrls = true; |
michael@0 | 34 | |
michael@0 | 35 | mPrefObserverId = PrefsHelper.getPrefs(prefs, new TitlePrefsHandler()); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | boolean shouldShowUrl() { |
michael@0 | 39 | return mShowUrl; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | boolean shouldTrimUrls() { |
michael@0 | 43 | return mTrimUrls; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | void close() { |
michael@0 | 47 | if (mPrefObserverId != null) { |
michael@0 | 48 | PrefsHelper.removeObserver(mPrefObserverId); |
michael@0 | 49 | mPrefObserverId = null; |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | private class TitlePrefsHandler extends PrefsHelper.PrefHandlerBase { |
michael@0 | 54 | @Override |
michael@0 | 55 | public void prefValue(String pref, String str) { |
michael@0 | 56 | // Handles PREF_TITLEBAR_MODE, which is always a string. |
michael@0 | 57 | int value = Integer.parseInt(str); |
michael@0 | 58 | boolean shouldShowUrl = (value == 1); |
michael@0 | 59 | |
michael@0 | 60 | if (shouldShowUrl == mShowUrl) { |
michael@0 | 61 | return; |
michael@0 | 62 | } |
michael@0 | 63 | mShowUrl = shouldShowUrl; |
michael@0 | 64 | |
michael@0 | 65 | triggerChangeListener(); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | @Override |
michael@0 | 69 | public void prefValue(String pref, boolean value) { |
michael@0 | 70 | // Handles PREF_TRIM_URLS, which should usually be a boolean. |
michael@0 | 71 | if (value == mTrimUrls) { |
michael@0 | 72 | return; |
michael@0 | 73 | } |
michael@0 | 74 | mTrimUrls = value; |
michael@0 | 75 | |
michael@0 | 76 | triggerChangeListener(); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | @Override |
michael@0 | 80 | public boolean isObserver() { |
michael@0 | 81 | // We want to be notified of changes to be able to switch mode |
michael@0 | 82 | // without restarting. |
michael@0 | 83 | return true; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | private void triggerChangeListener() { |
michael@0 | 87 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 88 | @Override |
michael@0 | 89 | public void run() { |
michael@0 | 90 | final Tabs tabs = Tabs.getInstance(); |
michael@0 | 91 | final Tab tab = tabs.getSelectedTab(); |
michael@0 | 92 | if (tab != null) { |
michael@0 | 93 | tabs.notifyListeners(tab, Tabs.TabEvents.TITLE); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | }); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | } |