summaryrefslogtreecommitdiffstats
path: root/win8/metro_driver/settings_handler.cc
blob: 6feae24320f7390c736f7cc2e3c33de095b6a67e (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
// Copyright (c) 2012 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.

#include "stdafx.h"
#include "settings_handler.h"

// This include allows to send WM_SYSCOMMANDs to chrome.
#include "chrome/app/chrome_command_ids.h"
#include "chrome_app_view.h"
#include "winrt_utils.h"

typedef winfoundtn::ITypedEventHandler<
    winui::ApplicationSettings::SettingsPane*,
    winui::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs*>
        CommandsRequestedHandler;

namespace {

// String identifiers for the settings pane commands.
const wchar_t* kSettingsId = L"settings";
const wchar_t* kHelpId = L"help";
const wchar_t* kAboutId = L"about";

}

SettingsHandler::SettingsHandler() {
  DVLOG(1) << __FUNCTION__;
}

SettingsHandler::~SettingsHandler() {
  DVLOG(1) << __FUNCTION__;
}

HRESULT SettingsHandler::Initialize() {
  mswr::ComPtr<winui::ApplicationSettings::ISettingsPaneStatics>
      settings_pane_statics;
  HRESULT hr = winrt_utils::CreateActivationFactory(
      RuntimeClass_Windows_UI_ApplicationSettings_SettingsPane,
      settings_pane_statics.GetAddressOf());
  CheckHR(hr, "Failed to activate ISettingsPaneStatics");

  mswr::ComPtr<winui::ApplicationSettings::ISettingsPane> settings_pane;
  hr = settings_pane_statics->GetForCurrentView(&settings_pane);
  CheckHR(hr, "Failed to get ISettingsPane");

  hr = settings_pane->add_CommandsRequested(
      mswr::Callback<CommandsRequestedHandler>(
          this,
          &SettingsHandler::OnSettingsCommandsRequested).Get(),
      &settings_token_);
  CheckHR(hr, "Failed to add CommandsRequested");

  return hr;
}

HRESULT SettingsHandler::OnSettingsCommandsRequested(
    winui::ApplicationSettings::ISettingsPane* settings_pane,
    winui::ApplicationSettings::ISettingsPaneCommandsRequestedEventArgs* args) {
  mswr::ComPtr<winui::ApplicationSettings::ISettingsCommandFactory>
      settings_command_factory;
  HRESULT hr = winrt_utils::CreateActivationFactory(
      RuntimeClass_Windows_UI_ApplicationSettings_SettingsCommand,
      settings_command_factory.GetAddressOf());
  CheckHR(hr, "Failed to activate ISettingsCommandFactory");

  mswr::ComPtr<winui::ApplicationSettings::ISettingsPaneCommandsRequest>
      settings_command_request;
  hr = args->get_Request(&settings_command_request);
  CheckHR(hr, "Failed to get_Request");

  mswr::ComPtr<SettingsHandler::ISettingsCommandVector> application_commands;
  hr = settings_command_request->get_ApplicationCommands(&application_commands);
  CheckHR(hr, "Failed to get_ApplicationCommands");

  // TODO(mad): Internationalize the hard coded user visible strings.
  hr = AppendNewSettingsCommand(
      kSettingsId, L"Settings", settings_command_factory.Get(),
      application_commands.Get());
  CheckHR(hr, "Failed to append new settings command");

  hr = AppendNewSettingsCommand(
      kHelpId, L"Help", settings_command_factory.Get(),
      application_commands.Get());
  CheckHR(hr, "Failed to append new help command");

  hr = AppendNewSettingsCommand(
      kAboutId, L"About", settings_command_factory.Get(),
      application_commands.Get());
  CheckHR(hr, "Failed to append new about command");

  return hr;
}

HRESULT SettingsHandler::AppendNewSettingsCommand(
    const wchar_t* id,
    const wchar_t* name,
    winui::ApplicationSettings::ISettingsCommandFactory*
        settings_command_factory,
    SettingsHandler::ISettingsCommandVector* settings_command_vector) {
  mswr::ComPtr<winfoundtn::IPropertyValue> settings_id;
  HRESULT hr = GetSettingsId(id, &settings_id);
  CheckHR(hr, "Can't get settings id");

  mswrw::HString settings_name;
  settings_name.Attach(MakeHString(name));
  mswr::ComPtr<winui::Popups::IUICommand> command;
  hr = settings_command_factory->CreateSettingsCommand(
      settings_id.Get(),
      settings_name.Get(),
      mswr::Callback<winui::Popups::IUICommandInvokedHandler>(
          &SettingsHandler::OnSettings).Get(),
      command.GetAddressOf());
  CheckHR(hr, "Can't create settings command");

  hr = settings_command_vector->Append(command.Get());
  CheckHR(hr, "Failed to append settings command");

  return hr;
}

HRESULT SettingsHandler::OnSettings(winui::Popups::IUICommand* command) {
  mswr::ComPtr<winfoundtn::IPropertyValue> settings_id;
  HRESULT hr = GetSettingsId(kSettingsId, &settings_id);
  CheckHR(hr, "Failed to get settings id");

  mswr::ComPtr<winfoundtn::IPropertyValue> help_id;
  hr = GetSettingsId(kHelpId, &help_id);
  CheckHR(hr, "Failed to get settings id");

  mswr::ComPtr<winfoundtn::IPropertyValue> about_id;
  hr = GetSettingsId(kAboutId, &about_id);
  CheckHR(hr, "Failed to get settings id");

  mswr::ComPtr<winfoundtn::IPropertyValue> command_id;
  hr = command->get_Id(&command_id);
  CheckHR(hr, "Failed to get command id");

  INT32 result = -1;
  hr = winrt_utils::CompareProperties(
      command_id.Get(), settings_id.Get(), &result);
  CheckHR(hr, "Failed to compare ids");

  HWND chrome_window = globals.host_windows.front().first;

  if (result == 0) {
    ::PostMessageW(chrome_window, WM_SYSCOMMAND, IDC_OPTIONS, 0);
    return S_OK;
  }

  hr = winrt_utils::CompareProperties(command_id.Get(), help_id.Get(), &result);
  CheckHR(hr, "Failed to compare ids");
  if (result == 0) {
    ::PostMessageW(chrome_window, WM_SYSCOMMAND, IDC_HELP_PAGE_VIA_MENU, 0);
    return S_OK;
  }

  hr = winrt_utils::CompareProperties(
      command_id.Get(), about_id.Get(), &result);
  CheckHR(hr, "Failed to compare ids");
  if (result == 0) {
    ::PostMessageW(chrome_window, WM_SYSCOMMAND, IDC_ABOUT, 0);
    return S_OK;
  }

  return S_OK;
}

HRESULT SettingsHandler::GetSettingsId(
    const wchar_t* value, winfoundtn::IPropertyValue** settings_id) {
  mswrw::HString property_value_string;
  property_value_string.Attach(MakeHString(value));
  return winrt_utils::CreateStringProperty(property_value_string.Get(),
                                           settings_id);
}