Skip to content

Commit

Permalink
Ooh, do the font size event too
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Mar 18, 2021
1 parent 801b296 commit 6ab8339
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 72 deletions.
46 changes: 27 additions & 19 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2290,29 +2290,37 @@ namespace winrt::TerminalApp::implementation
return false;
}

void TerminalPage::_ControlNoticeRaisedHandler(const IInspectable /*sender*/, const Microsoft::Terminal::Control::NoticeEventArgs eventArgs)
// Important! Don't take this eventArgs by reference, we need to extend the
// lifetime of it to the other side of the co_await!
winrt::fire_and_forget TerminalPage::_ControlNoticeRaisedHandler(const IInspectable /*sender*/,
const Microsoft::Terminal::Control::NoticeEventArgs eventArgs)
{
winrt::hstring message = eventArgs.Message();
auto weakThis = get_weak();
co_await winrt::resume_foreground(Dispatcher());
if (auto page = weakThis.get())
{
winrt::hstring message = eventArgs.Message();

winrt::hstring title;
winrt::hstring title;

switch (eventArgs.Level())
{
case NoticeLevel::Debug:
title = RS_(L"NoticeDebug"); //\xebe8
break;
case NoticeLevel::Info:
title = RS_(L"NoticeInfo"); // \xe946
break;
case NoticeLevel::Warning:
title = RS_(L"NoticeWarning"); //\xe7ba
break;
case NoticeLevel::Error:
title = RS_(L"NoticeError"); //\xe783
break;
}
switch (eventArgs.Level())
{
case NoticeLevel::Debug:
title = RS_(L"NoticeDebug"); //\xebe8
break;
case NoticeLevel::Info:
title = RS_(L"NoticeInfo"); // \xe946
break;
case NoticeLevel::Warning:
title = RS_(L"NoticeWarning"); //\xe7ba
break;
case NoticeLevel::Error:
title = RS_(L"NoticeError"); //\xe783
break;
}

_ShowControlNoticeDialog(title, message);
page->_ShowControlNoticeDialog(title, message);
}
}

void TerminalPage::_ShowControlNoticeDialog(const winrt::hstring& title, const winrt::hstring& message)
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ namespace winrt::TerminalApp::implementation

void _PasteText();

void _ControlNoticeRaisedHandler(const IInspectable sender, const Microsoft::Terminal::Control::NoticeEventArgs eventArgs);
winrt::fire_and_forget _ControlNoticeRaisedHandler(const IInspectable sender, const Microsoft::Terminal::Control::NoticeEventArgs eventArgs);
void _ShowControlNoticeDialog(const winrt::hstring& title, const winrt::hstring& message);

fire_and_forget _LaunchSettings(const Microsoft::Terminal::Settings::Model::SettingsTarget target);
Expand Down
39 changes: 12 additions & 27 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,44 +524,29 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// Arguments:
// - initialUpdate: whether this font update should be considered as being
// concerned with initialization process. Value forwarded to event handler.
void ControlCore::_UpdateFont(const bool /*initialUpdate*/)
void ControlCore::_UpdateFont(const bool initialUpdate)
{
const int newDpi = static_cast<int>(static_cast<double>(USER_DEFAULT_SCREEN_DPI) *
_compositionScaleX);

// !TODO!: MSFT:20895307 If the font doesn't exist, this doesn't
// TODO: MSFT:20895307 If the font doesn't exist, this doesn't
// actually fail. We need a way to gracefully fallback.
_renderer->TriggerFontChange(newDpi, _desiredFont, _actualFont);

// If the actual font isn't what was requested...
if (_actualFont.GetFaceName() != _desiredFont.GetFaceName())
{
// !TODO!: We _DO_ want this
// // Then warn the user that we picked something because we couldn't find their font.
// // Format message with user's choice of font and the font that was chosen instead.
// const winrt::hstring message{ fmt::format(std::wstring_view{ RS_(L"NoticeFontNotFound") }, _desiredFont.GetFaceName(), _actualFont.GetFaceName()) };
// // Capture what we need to resume later.
// [strongThis = get_strong(), message]() -> winrt::fire_and_forget {
// // Take these out of the lambda and store them locally
// // because the coroutine will lose them into space
// // by the time it resumes.
// const auto msg = message;
// const auto strong = strongThis;

// // Pop the rest of this function to the tail of the UI thread
// // Just in case someone was holding a lock when they called us and
// // the handlers decide to do something that take another lock
// // (like ShellExecute pumping our messaging thread...GH#7994)
// co_await strong->Dispatcher();

// auto noticeArgs = winrt::make<NoticeEventArgs>(NoticeLevel::Warning, std::move(msg));
// strong->_raiseNoticeHandlers(*strong, std::move(noticeArgs));
// }();
// Then warn the user that we picked something because we couldn't find their font.
// Format message with user's choice of font and the font that was chosen instead.
const winrt::hstring message{ fmt::format(std::wstring_view{ RS_(L"NoticeFontNotFound") },
_desiredFont.GetFaceName(),
_actualFont.GetFaceName()) };
auto noticeArgs = winrt::make<NoticeEventArgs>(NoticeLevel::Warning, message);
_RaiseNoticeHandlers(*this, std::move(noticeArgs));
}

// !TODO!: We _DO_ want this
// const auto actualNewSize = _actualFont.GetSize();
// _fontSizeChangedHandlers(actualNewSize.X, actualNewSize.Y, initialUpdate);
const auto actualNewSize = _actualFont.GetSize();
_FontSizeChangedHandlers(actualNewSize.X, actualNewSize.Y, initialUpdate);
}

// Method Description:
Expand Down Expand Up @@ -776,7 +761,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// save location (for rendering) + render
_terminal->SetSelectionEnd(terminalPosition);
_renderer->TriggerSelection();
// _selectionNeedsToBeCopied = true;
// _selectionNeedsToBeCopied = true; // !TODO! why is this commented out?
}

// Called when the Terminal wants to set something to the clipboard, i.e.
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation

// -------------------------------- WinRT Events ---------------------------------
// clang-format off
WINRT_CALLBACK(FontSizeChanged, Control::FontSizeChangedEventArgs);

TYPED_EVENT(CopyToClipboard, IInspectable, Control::CopyToClipboardEventArgs);
TYPED_EVENT(TitleChanged, IInspectable, Control::TitleChangedEventArgs);
TYPED_EVENT(WarningBell, IInspectable, IInspectable);
Expand All @@ -134,6 +136,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
TYPED_EVENT(RendererEnteredErrorState, IInspectable, IInspectable);
TYPED_EVENT(SwapChainChanged, IInspectable, IInspectable);
TYPED_EVENT(RendererWarning, IInspectable, Control::RendererWarningArgs);
TYPED_EVENT(RaiseNotice, IInspectable, Control::NoticeEventArgs);
// clang-format on

private:
Expand Down
32 changes: 8 additions & 24 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,6 @@ DEFINE_ENUM_FLAG_OPERATORS(winrt::Microsoft::Terminal::Control::CopyFormat);

namespace winrt::Microsoft::Terminal::Control::implementation
{
// Helper static function to ensure that all ambiguous-width glyphs are reported as narrow.
// See microsoft/terminal#2066 for more info.
static bool _IsGlyphWideForceNarrowFallback(const std::wstring_view /* glyph */)
{
return false; // glyph is not wide.
}

static bool _EnsureStaticInitialization()
{
// use C++11 magic statics to make sure we only do this once.
static bool initialized = []() {
// *** THIS IS A SINGLETON ***
SetGlyphWidthFallback(_IsGlyphWideForceNarrowFallback);

return true;
}();
return initialized;
}

TermControl::TermControl(IControlSettings settings,
TerminalConnection::ITerminalConnection connection) :
_initializedTerminal{ false },
Expand All @@ -80,7 +61,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_selectionNeedsToBeCopied{ false },
_searchBox{ nullptr }
{
_EnsureStaticInitialization();
InitializeComponent();

_core = winrt::make_self<ControlCore>(settings, connection);
Expand All @@ -89,6 +69,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_core->ScrollPositionChanged({ get_weak(), &TermControl::_ScrollPositionChanged });
_core->CursorPositionChanged({ get_weak(), &TermControl::_CursorPositionChanged });
_core->RendererEnteredErrorState({ get_weak(), &TermControl::_RendererEnteredErrorState });
_core->FontSizeChanged({ get_weak(), &TermControl::_coreFontSizeChanged });

// Initialize the terminal only once the swapchainpanel is loaded - that
// way, we'll be able to query the real pixel size it got on layout
Expand Down Expand Up @@ -2729,8 +2710,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
}
}

// -------------------------------- WinRT Events ---------------------------------
// Winrt events need a method for adding a callback to the event and removing the callback.
// These macros will define them both for you.
DEFINE_EVENT(TermControl, FontSizeChanged, _fontSizeChangedHandlers, Control::FontSizeChangedEventArgs);
void TermControl::_coreFontSizeChanged(const int fontWidth,
const int fontHeight,
const bool isInitialChange)
{
_FontSizeChangedHandlers(fontWidth, fontHeight, isInitialChange);
}

}
6 changes: 5 additions & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation

// -------------------------------- WinRT Events ---------------------------------
// clang-format off
DECLARE_EVENT(FontSizeChanged, _fontSizeChangedHandlers, Control::FontSizeChangedEventArgs);
WINRT_CALLBACK(FontSizeChanged, Control::FontSizeChangedEventArgs);

FORWARDED_TYPED_EVENT(CopyToClipboard, IInspectable, Control::CopyToClipboardEventArgs, _core, CopyToClipboard);
FORWARDED_TYPED_EVENT(TitleChanged, IInspectable, Control::TitleChangedEventArgs, _core, TitleChanged);
Expand Down Expand Up @@ -245,6 +245,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation

winrt::fire_and_forget _RaiseReadOnlyWarning();
winrt::fire_and_forget _hoveredHyperlinkChanged(const IInspectable& sender, const IInspectable& args);

void _coreFontSizeChanged(const int fontWidth,
const int fontHeight,
const bool isInitialChange);
};
}

Expand Down

1 comment on commit 6ab8339

@github-actions

This comment was marked as outdated.

Please sign in to comment.