Skip to content

Commit

Permalink
config: add default ime override feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhngtu committed Jan 15, 2024
1 parent 14c323c commit 4f7d148
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
17 changes: 13 additions & 4 deletions VietTypeConfig/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

using System;
using System.Resources;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using VietTypeConfig.Properties;
using static VietTypeConfig.VietTypeRegistrar;

namespace VietTypeConfig {
public partial class Form1 : Form {
private readonly ResourceManager rm = new ResourceManager(typeof(Form1));
private bool oobe = false;

public Form1() {
public Form1(string[] args) {
if (args.Length > 0) {
oobe = string.Equals(args[0], "-oobe", StringComparison.InvariantCultureIgnoreCase);
}
InitializeComponent();
udOptimizeMultilang.MouseWheel += (sender, e) => ((HandledMouseEventArgs)e).Handled = true;
}
Expand Down Expand Up @@ -64,6 +66,13 @@ private void Activate(bool newState) {
var result = newState ? ActivateProfiles() : DeactivateProfiles();
if (result == S_OK) {
if (newState) {
if (MessageBox.Show(rm.GetString("setDefaultQuestion"), "VietType", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
try {
Settings.SetDefault();
} catch (Exception ex) {
MessageBox.Show(string.Format(rm.GetString("cannotSaveSettingsMessage"), ex.Message), "VietType", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
MessageBox.Show(rm.GetString("enableSuccessMessage"), "VietType", MessageBoxButtons.OK, MessageBoxIcon.Information);
} else {
MessageBox.Show(rm.GetString("disableSuccessMessage"), "VietType", MessageBoxButtons.OK, MessageBoxIcon.Information);
Expand All @@ -77,7 +86,7 @@ private void Activate(bool newState) {

private void Form1_Shown(object sender, EventArgs e) {
if (IsProfileActivated() != S_OK) {
if (MessageBox.Show(rm.GetString("notEnabledQuestion"), "VietType", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
if (oobe || MessageBox.Show(rm.GetString("notEnabledQuestion"), "VietType", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
Activate(true);
}
}
Expand Down
3 changes: 3 additions & 0 deletions VietTypeConfig/Form1.en.resx
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,7 @@ VietType is free software, licensed under the GNU General Public License. VietTy
<data name="notEnabledQuestion" xml:space="preserve">
<value>VietType is not enabled. Enable it now?</value>
</data>
<data name="setDefaultQuestion" xml:space="preserve">
<value>Set VietType as default input method?</value>
</data>
</root>
3 changes: 3 additions & 0 deletions VietTypeConfig/Form1.vi.resx
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,7 @@ VietType là phần mềm tự do, bạn có thể cung cấp lại và/hoặc c
<data name="lblOptimizeMultilang.Text" xml:space="preserve">
<value>Mức tối ưu gõ nhiều ngôn ngữ (&amp;O):</value>
</data>
<data name="setDefaultQuestion" xml:space="preserve">
<value>Đặt VietType làm bộ gõ mặc định?</value>
</data>
</root>
5 changes: 5 additions & 0 deletions VietTypeConfig/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ internal static class VietTypeRegistrar {
[DllImport("VietTypeATL32.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int IsProfileActivated();
}

internal static class NativeMethods {
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetInputMethodOverride(string TipString);
}
}
4 changes: 2 additions & 2 deletions VietTypeConfig/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ internal static class Program {
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Application.Run(new Form1(args));
}
}
}
7 changes: 7 additions & 0 deletions VietTypeConfig/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ internal class Settings : INotifyPropertyChanged {
private const string Subkey = "Software\\VietType";
private static readonly Guid GUID_SystemNotifyCompartment = Guid.Parse("{B2FBD2E7-922F-4996-BE77-21085B91A8F0}");
private static readonly Guid CLSID_TF_ThreadMgr = Guid.Parse("{529a9e6b-6587-4f23-ab9e-9c7d683e3c50}");
private static readonly Guid CLSID_TextService = Guid.Parse("{c0dd01a1-0deb-454b-8b42-d22ced1b4b23}");
private static readonly Guid GUID_Profile = Guid.Parse("{8D93D10A-203B-4C5F-A122-8898EF9C56F5}");
private const ushort TextServiceLangId = 0x42a;

bool default_enabled = false;
public bool DefaultEnabled {
Expand Down Expand Up @@ -147,5 +150,9 @@ public static void SaveSettings(Settings settings) {
}
}
}

public static void SetDefault() {
NativeMethods.SetInputMethodOverride(string.Format("{0:X4}:{{{1}}}{{{2}}}", TextServiceLangId, CLSID_TextService.ToString(), GUID_Profile.ToString()));
}
}
}

0 comments on commit 4f7d148

Please sign in to comment.