Skip to content

Commit

Permalink
User/lamotile/add_powerpreviewsettings (microsoft#1075)
Browse files Browse the repository at this point in the history
* Added powersettings to PowerToys Settings UI

* added settings library

* updated settings-web

* updated project oncfiguration

* updated project onfiguration

* updated project .sln file

* removed .etl file and added it to git-ignore

* separated the PowerPreviewModule into split classes .cpp and .h

* moved PowerPreviewModule implemnetations to .cpp file

* fixed StringTable formatter

* fixed spacing in resource.h

* added m_ to member varibales

* initiliaze m_isPreviewEnabled in the base class

* removed duplication of objects by using pass by refference and std::move

* made the getters const

* updated naming convention

* Split test calsses

* Add const string

* Replaced move with const string

* Made attributes private

* Made attributes private

* removed unused constructor

* Update resource.h

formatted resource.h
  • Loading branch information
Lavius Motileng authored and udit3333 committed Feb 19, 2020
1 parent 782c97a commit 8dac06f
Show file tree
Hide file tree
Showing 45 changed files with 3,362 additions and 1,832 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,6 @@ ASALocalRun/
# Temp build files
src/settings/settings-html/200.html
src/settings/settings-html/404.html

# Temp telemetry files.
src/common/Telemetry/*.etl
17 changes: 17 additions & 0 deletions src/common/UnitTests-CommonLib/Settings.Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,23 @@ namespace UnitTestsCommonLib
compareJsons(expected, actual);
}

TEST_METHOD(SettingsAddLargeHeader)
{
const auto value = L"large header sample text ";

Settings settings(nullptr, m_moduleName);
settings.add_header_szLarge(m_defaultSettingsName, m_defaultSettingsDescription, value);

auto expected = m_defaultSettingsJson;
auto expectedProperties = createSettingsProperties(L"header_large");
expectedProperties.SetNamedValue(L"value", json::JsonValue::CreateStringValue(value));
expected.GetNamedObject(L"properties").SetNamedValue(m_defaultSettingsName, expectedProperties);

const auto actual = json::JsonObject::Parse(settings.serialize());

compareJsons(expected, actual);
}

TEST_METHOD(SettingsAddStringMultiline)
{
const auto value = L"Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident,\nsunt in culpa qui officia deserunt mollit anim id est laborum.";
Expand Down
10 changes: 10 additions & 0 deletions src/common/settings_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ namespace PowerToysSettings {
m_json.GetNamedObject(L"properties").SetNamedValue(name, ml_string);
}

void Settings::add_header_szLarge(std::wstring_view name, std::wstring_view description, std::wstring_view value){
json::JsonObject string;
string.SetNamedValue(L"display_name", json::value(description));
string.SetNamedValue(L"editor_type", json::value(L"header_large"));
string.SetNamedValue(L"value", json::value(value));
string.SetNamedValue(L"order", json::value(++m_curr_priority));

m_json.GetNamedObject(L"properties").SetNamedValue(name, string);
}

// add_color_picker overloads.
void Settings::add_color_picker(std::wstring_view name, UINT description_resource_id, std::wstring_view value) {
add_color_picker(name, get_resource(description_resource_id), value);
Expand Down
8 changes: 4 additions & 4 deletions src/common/settings_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace PowerToysSettings {
void add_custom_action(std::wstring_view name, UINT description_resource_id, UINT button_text_resource_id, std::wstring_view value);
void add_custom_action(std::wstring_view name, std::wstring_view description, std::wstring_view button_text, std::wstring_view value);


void add_header_szLarge(std::wstring_view name, std::wstring_view description, std::wstring_view value);
// Serialize the internal json to a string.
std::wstring serialize();
// Serialize the internal json to the input buffer.
Expand All @@ -73,8 +73,8 @@ namespace PowerToysSettings {
template <typename T>
inline void add_property(std::wstring_view name, T value)
{
json::JsonObject prop_value;
prop_value.SetNamedValue(L"value", json::value(value));
json::JsonObject prop_value;
prop_value.SetNamedValue(L"value", json::value(value));
m_json.GetNamedObject(L"properties").SetNamedValue(name, prop_value);
}

Expand Down Expand Up @@ -108,7 +108,7 @@ namespace PowerToysSettings {
json::JsonObject m_json;
};

class HotkeyObject {
class HotkeyObject {
public:
static HotkeyObject from_json(json::JsonObject json) {
return HotkeyObject(std::move(json));
Expand Down
1 change: 1 addition & 0 deletions src/modules/example_powertoy/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ void Trace::MyEvent()
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}

28 changes: 28 additions & 0 deletions src/modules/previewpane/powerpreview/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "pch.h"
#include <interface/powertoy_module_interface.h>
#include "trace.h"
#include "powerpreview.h"


BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
Trace::RegisterProvider();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
Trace::UnregisterProvider();
break;
}
return TRUE;
}


extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
{
return new PowerPreviewModule();
}
3 changes: 3 additions & 0 deletions src/modules/previewpane/powerpreview/pch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "pch.h"
#pragma comment(lib, "windowsapp")
#pragma comment(lib, "shlwapi.lib")
5 changes: 5 additions & 0 deletions src/modules/previewpane/powerpreview/pch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Shlwapi.h>
#include <ProjectTelemetry.h>
135 changes: 135 additions & 0 deletions src/modules/previewpane/powerpreview/powerpreview.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "pch.h"
#include <interface/lowlevel_keyboard_event_data.h>
#include <interface/win_hook_event_data.h>
#include <settings_objects.h>
#include <common.h>
#include "powerpreview.h"
#include "trace.h"
#include "settings.h"
#include "resource.h"

// Destroy the powertoy and free memory.
void PowerPreviewModule::destroy()
{
Trace::Destroyed();
delete this;
}

// Return the display name of the powertoy, this will be cached.
const wchar_t* PowerPreviewModule::get_name()
{
return m_moduleName.c_str();
}

const wchar_t** PowerPreviewModule::get_events()
{
return nullptr;
}

// Return JSON with the configuration options.
bool PowerPreviewModule::get_config(_Out_ wchar_t* buffer, _Out_ int* buffer_size)
{
HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);

// Create a Settings object.
PowerToysSettings::Settings settings(hinstance, get_name());

// General Settings.
settings.set_description(GET_RESOURCE_STRING(IDS_GENERAL_DESCRIPTION));
settings.set_icon_key(GET_RESOURCE_STRING(IDS_ICON_KEY_NAME));

// Explorer: Settings Group Header.
settings.add_header_szLarge(
GET_RESOURCE_STRING(IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_HEADER_ID),
GET_RESOURCE_STRING(IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_DESC),
GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT));

// Explorer: SVG Icon Settings.
settings.add_bool_toogle(
m_explrSVGSettings.GetName(),
m_explrSVGSettings.GetDescription(),
m_explrSVGSettings.GetState());

// Preview Pane: Settings Group Header.
settings.add_header_szLarge(
GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_HEADER_ID),
GET_RESOURCE_STRING(IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_DESC),
GET_RESOURCE_STRING(IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_TEXT));

// Preview Pane: SVG Settings.
settings.add_bool_toogle(
m_prevPaneSVGSettings.GetName(),
m_prevPaneSVGSettings.GetDescription(),
m_prevPaneSVGSettings.GetState());

// Preview Pane: Mark Down Settings.
settings.add_bool_toogle(
m_prevPaneMDSettings.GetName(),
m_prevPaneMDSettings.GetDescription(),
m_prevPaneMDSettings.GetState());

return settings.serialize_to_buffer(buffer, buffer_size);
}

// Called by the runner to pass the updated settings values as a serialized JSON.
void PowerPreviewModule::set_config(const wchar_t* config)
{
try
{
PowerToysSettings::PowerToyValues values = PowerToysSettings::PowerToyValues::from_json_string(config);
m_explrSVGSettings.UpdateState(values);
m_prevPaneSVGSettings.UpdateState(values);
m_prevPaneMDSettings.UpdateState(values);
values.save_to_settings_file();
}
catch (std::exception const& e)
{
Trace::SetConfigInvalidJSON(e.what());
}
}

// Enable the powertoy
void PowerPreviewModule::enable()
{
this->m_enabled = true;
Trace::FilePreviewerIsEnabled();
}

// Disable the powertoy
void PowerPreviewModule::disable()
{
this->m_enabled = false;
Trace::FilePreviewerIsDisabled();
}

// Returns if the powertoys is enabled
bool PowerPreviewModule::is_enabled()
{
return this->m_enabled;
}

// Handle incoming event, data is event-specific
intptr_t PowerPreviewModule::signal_event(const wchar_t* name, intptr_t data)
{
return 0;
}

// Load the settings file.
void PowerPreviewModule::init_settings()
{
try
{
// Load and parse the settings file for this PowerToy.
PowerToysSettings::PowerToyValues settings =
PowerToysSettings::PowerToyValues::load_from_settings_file(PowerPreviewModule::get_name());

// Load settings states.
m_explrSVGSettings.LoadState(settings);
m_prevPaneSVGSettings.LoadState(settings);
m_prevPaneMDSettings.LoadState(settings);
}
catch (std::exception const& e)
{
Trace::InitSetErrorLoadingFile(e.what());
}
}
45 changes: 45 additions & 0 deletions src/modules/previewpane/powerpreview/powerpreview.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "pch.h"
#include <interface/powertoy_module_interface.h>
#include <common.h>
#include "trace.h"
#include "settings.h"

using namespace PowerPreviewSettings;

extern "C" IMAGE_DOS_HEADER __ImageBase;

// Implement the PowerToy Module Interface and all the required methods.
class PowerPreviewModule : public PowertoyModuleIface
{
private:
// The PowerToy state.
bool m_enabled = false;
ExplrSVGSttngs m_explrSVGSettings;
PrevPaneSVGRendrSettings m_prevPaneSVGSettings;
PrevPaneMDRendrSettings m_prevPaneMDSettings;
std::wstring m_moduleName;

public:
PowerPreviewModule()
:
m_moduleName(GET_RESOURCE_STRING(IDS_MODULE_NAME)),
m_explrSVGSettings(ExplrSVGSttngs()),
m_prevPaneSVGSettings(PrevPaneSVGRendrSettings()),
m_prevPaneMDSettings(PrevPaneMDRendrSettings())
{
init_settings();
};

virtual void destroy();
virtual const wchar_t* get_name();
virtual const wchar_t** get_events();
virtual bool get_config(_Out_ wchar_t* buffer, _Out_ int* buffer_size);
virtual void set_config(const wchar_t* config);
virtual void enable();
virtual void disable();
virtual bool is_enabled();
virtual void init_settings();
virtual intptr_t signal_event(const wchar_t* name, intptr_t data);
virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) override {}
virtual void signal_system_menu_action(const wchar_t* name) override {}
};
76 changes: 76 additions & 0 deletions src/modules/previewpane/powerpreview/powerpreview.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (United States) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END

3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END

#endif // APSTUDIO_INVOKED

#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
STRINGTABLE
BEGIN
IDS_GENERAL_DESCRIPTION L"These settings allow you to manage your Windows File Explorer Addons.";
IDS_MODULE_NAME L"File Explorer Preview";
IDS_ICON_KEY_NAME L"pt-power-preview";
IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_HEADER_ID L"EXPLR_ICONS_PREV_STTNGS_GROUP_HEADER_ID";
IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_DESC L"Settings Group Header Text";
IDS_EXPLR_ICONS_PREV_STTNGS_GROUP_TEXT L"Explorer Icons";
IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_HEADER_ID L"PRVPANE_FILE_PREV_STTNGS_GROUP_HEADER_ID";
IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_DESC L"Settings Group Header Text";
IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT L"Preview Pane";
IDS_PREVPANE_MD_BOOL_TOGGLE_CONTROLL L"PREVPANE_MD_BOOL_TOGGLE_CONTROLL_ID"
IDS_PREVPANE_MD_SETTINGS_DESCRIPTION L"Show Markdown"
IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL L"IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL"
IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION L"Show SVG"
IDS_EXPLR_SVG_BOOL_TOGGLE_CONTROLL L"EXPLR_SVG_BOOL_TOGGLE_CONTROLL"
IDS_EXPLR_SVG_SETTINGS_DESCRIPTION L"Render SVG images"
END


#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
Loading

0 comments on commit 8dac06f

Please sign in to comment.