Skip to content

Commit

Permalink
Add "Fixed right edge positioning" and "Fixed bottom edge positioning…
Browse files Browse the repository at this point in the history
…" options for the popup
  • Loading branch information
rampaa committed Dec 29, 2024
1 parent eba0fed commit fe11f95
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
12 changes: 12 additions & 0 deletions JL.Windows/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ internal sealed class ConfigManager
public bool PopupDynamicHeight { get; private set; } = true;
public bool PopupDynamicWidth { get; private set; } = true;
public bool FixedPopupPositioning { get; private set; } // = false;
public bool FixedPopupRightPositioning { get; private set; } // = false;
public double FixedPopupXPosition { get; set; } // = 0;
public bool FixedPopupBottomPositioning { get; private set; } // = false;
public double FixedPopupYPosition { get; set; } // = 0;
public bool PopupFocusOnLookup { get; private set; } // = false;
public bool ShowMiningModeReminder { get; private set; } = true;
Expand Down Expand Up @@ -314,6 +316,8 @@ public void ApplyPreferences(SqliteConnection connection)
ShowMiningModeReminder = ConfigDBManager.GetValueFromConfig(connection, ShowMiningModeReminder, nameof(ShowMiningModeReminder), bool.TryParse);
DisableLookupsForNonJapaneseCharsInPopups = ConfigDBManager.GetValueFromConfig(connection, DisableLookupsForNonJapaneseCharsInPopups, nameof(DisableLookupsForNonJapaneseCharsInPopups), bool.TryParse);
FixedPopupPositioning = ConfigDBManager.GetValueFromConfig(connection, FixedPopupPositioning, nameof(FixedPopupPositioning), bool.TryParse);
FixedPopupRightPositioning = ConfigDBManager.GetValueFromConfig(connection, FixedPopupRightPositioning, nameof(FixedPopupRightPositioning), bool.TryParse);
FixedPopupBottomPositioning = ConfigDBManager.GetValueFromConfig(connection, FixedPopupBottomPositioning, nameof(FixedPopupBottomPositioning), bool.TryParse);
ChangeMainWindowBackgroundOpacityOnUnhover = ConfigDBManager.GetValueFromConfig(connection, ChangeMainWindowBackgroundOpacityOnUnhover, nameof(ChangeMainWindowBackgroundOpacityOnUnhover), bool.TryParse);
TextOnlyVisibleOnHover = ConfigDBManager.GetValueFromConfig(connection, TextOnlyVisibleOnHover, nameof(TextOnlyVisibleOnHover), bool.TryParse);
OnlyCaptureTextWithJapaneseChars = ConfigDBManager.GetValueFromConfig(connection, OnlyCaptureTextWithJapaneseChars, nameof(OnlyCaptureTextWithJapaneseChars), bool.TryParse);
Expand Down Expand Up @@ -929,6 +933,8 @@ public void LoadPreferenceWindow(PreferencesWindow preferenceWindow)
preferenceWindow.PopupMinHeightNumericUpDown.Value = PopupMinHeight;
preferenceWindow.PopupMinWidthNumericUpDown.Value = PopupMinWidth;
preferenceWindow.FixedPopupPositioningCheckBox.IsChecked = FixedPopupPositioning;
preferenceWindow.FixedPopupRightPositioningCheckBox.IsChecked = FixedPopupRightPositioning;
preferenceWindow.FixedPopupBottomPositioningCheckBox.IsChecked = FixedPopupBottomPositioning;
preferenceWindow.FixedPopupXPositionNumericUpDown.Value = FixedPopupXPosition;
preferenceWindow.FixedPopupYPositionNumericUpDown.Value = FixedPopupYPosition;
preferenceWindow.PopupDynamicHeightCheckBox.IsChecked = PopupDynamicHeight;
Expand Down Expand Up @@ -1266,6 +1272,12 @@ public async Task SavePreferences(PreferencesWindow preferenceWindow)
ConfigDBManager.UpdateSetting(connection, nameof(FixedPopupPositioning),
preferenceWindow.FixedPopupPositioningCheckBox.IsChecked.ToString()!);

ConfigDBManager.UpdateSetting(connection, nameof(FixedPopupRightPositioning),
preferenceWindow.FixedPopupRightPositioningCheckBox.IsChecked.ToString()!);

ConfigDBManager.UpdateSetting(connection, nameof(FixedPopupBottomPositioning),
preferenceWindow.FixedPopupBottomPositioningCheckBox.IsChecked.ToString()!);

ConfigDBManager.UpdateSetting(connection, nameof(FixedPopupXPosition),
preferenceWindow.FixedPopupXPositionNumericUpDown.Value.ToString(CultureInfo.InvariantCulture));

Expand Down
56 changes: 50 additions & 6 deletions JL.Windows/GUI/PopupWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using JL.Windows.SpeechSynthesis;
using JL.Windows.Utilities;
using Rectangle = System.Drawing.Rectangle;
using Screen = System.Windows.Forms.Screen;

namespace JL.Windows.GUI;

Expand Down Expand Up @@ -220,7 +221,7 @@ public Task LookupOnCharPosition(TextBox textBox, int charPosition, bool enableM
{
if (configManager.FixedPopupPositioning && this == mainWindow.FirstPopupWindow)
{
UpdatePosition(configManager.FixedPopupXPosition, configManager.FixedPopupYPosition);
UpdatePosition();
}

else
Expand Down Expand Up @@ -282,7 +283,7 @@ public Task LookupOnCharPosition(TextBox textBox, int charPosition, bool enableM

if (configManager.FixedPopupPositioning && this == mainWindow.FirstPopupWindow)
{
UpdatePosition(configManager.FixedPopupXPosition, configManager.FixedPopupYPosition);
UpdatePosition();
}

else
Expand Down Expand Up @@ -348,7 +349,7 @@ public Task LookupOnSelect(TextBox textBox)
{
if (configManager.FixedPopupPositioning && this == mainWindow.FirstPopupWindow)
{
UpdatePosition(configManager.FixedPopupXPosition, configManager.FixedPopupYPosition);
UpdatePosition();
}

else
Expand Down Expand Up @@ -391,7 +392,7 @@ public Task LookupOnSelect(TextBox textBox)

if (configManager.FixedPopupPositioning && this == mainWindow.FirstPopupWindow)
{
UpdatePosition(configManager.FixedPopupXPosition, configManager.FixedPopupYPosition);
UpdatePosition();
}

else
Expand Down Expand Up @@ -480,8 +481,51 @@ private void UpdatePosition(Point cursorPosition)
WinApi.MoveWindowToPosition(WindowHandle, newLeft, newTop);
}

private void UpdatePosition(double x, double y)
private void UpdatePosition()
{
Screen activeScreen = WindowsUtils.ActiveScreen;
ConfigManager configManager = ConfigManager.Instance;

double x = configManager.FixedPopupXPosition;
if (configManager.FixedPopupRightPositioning)
{
double currentWidth = ActualWidth * WindowsUtils.Dpi.DpiScaleX;
if (x is 0)
{
x = (activeScreen.Bounds.Left + activeScreen.Bounds.Right + currentWidth) / 2;
}
else if (x is -1)
{
x = activeScreen.WorkingArea.Right;
}
else if (x is -2)
{
x = activeScreen.Bounds.Right;
}

x = Math.Max(x is -1 ? activeScreen.WorkingArea.Left : activeScreen.Bounds.Left, x - currentWidth);
}

double y = configManager.FixedPopupYPosition;
if (configManager.FixedPopupBottomPositioning)
{
double currentHeight = ActualHeight * WindowsUtils.Dpi.DpiScaleY;
if (y is -2)
{
y = activeScreen.Bounds.Bottom;
}
else if (y is -1)
{
y = activeScreen.WorkingArea.Bottom;
}
else if (y is 0)
{
y = (activeScreen.Bounds.Top + activeScreen.Bounds.Bottom + currentHeight) / 2;
}

y = Math.Max(y is -1 ? activeScreen.WorkingArea.Top : activeScreen.Bounds.Top, y - currentHeight);
}

WinApi.MoveWindowToPosition(WindowHandle, x, y);
}

Expand Down Expand Up @@ -1698,7 +1742,7 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e)

else if (configManager.FixedPopupPositioning && this == mainWindow.FirstPopupWindow)
{
UpdatePosition(configManager.FixedPopupXPosition, configManager.FixedPopupYPosition);
UpdatePosition();
}

else
Expand Down
28 changes: 28 additions & 0 deletions JL.Windows/GUI/PreferencesWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,20 @@
<CheckBox x:Name="FixedPopupPositioningCheckBox" HorizontalAlignment="Right" />
</DockPanel>

<DockPanel>
<TextBlock HorizontalAlignment="Left" Text="Fixed right edge positioning"
Style="{StaticResource TextBlockDefault}"
Cursor="Help" ToolTip="&quot;Fixed positioning&quot; option must be enabled for this to take effect.
&#10;When enabled, the &quot;Fixed X position&quot; option will be used to specify the right edge position of the popup.
&#10;When this option is enabled and &quot;Fixed X position&quot; is set to:
&#10;-2, the popup will be positioned at the right edge of the screen.
&#10;-1, the popup will be positioned at the right edge of the working area.
&#10;0, the popup will be centered horizontally on the screen.
&#10;Any other value: Sets the popup's right edge at that specified number of pixels from the left edge of the active screen."
TextWrapping="Wrap" VerticalAlignment="Center" />
<CheckBox x:Name="FixedPopupRightPositioningCheckBox" HorizontalAlignment="Right" />
</DockPanel>

<DockPanel>
<TextBlock HorizontalAlignment="Left" Text="Fixed X position"
Style="{StaticResource TextBlockDefault}"
Expand All @@ -703,6 +717,20 @@
HorizontalAlignment="Right" />
</DockPanel>

<DockPanel>
<TextBlock HorizontalAlignment="Left" Text="Fixed bottom edge positioning"
Style="{StaticResource TextBlockDefault}"
Cursor="Help" ToolTip="&quot;Fixed positioning&quot; option must be enabled for this to take effect.
&#10;When enabled, the &quot;Fixed Y position&quot; option will be used to specify the bottom edge position of the popup.
&#10;When this option is enabled and &quot;Fixed Y position&quot; is set to:
&#10;-2, the popup will be positioned at the bottom edge of the screen.
&#10;-1, the popup will be positioned at the bottom edge of the working area.
&#10;0, the popup will be centered vertically on the screen.
&#10;Any other value: Sets the popup's bottom edge at that specified number of pixels from the top edge of the active screen."
TextWrapping="Wrap" VerticalAlignment="Center" />
<CheckBox x:Name="FixedPopupBottomPositioningCheckBox" HorizontalAlignment="Right" />
</DockPanel>

<DockPanel>
<TextBlock HorizontalAlignment="Left" Text="Fixed Y position"
Style="{StaticResource TextBlockDefault}"
Expand Down

0 comments on commit fe11f95

Please sign in to comment.