How do I dismiss OSX Character Picker Palette from code - swift

I can present the Character Picker Palette (emoji) with:
NSApplication.shared.orderFrontCharacterPalette(self)
But I can't find any way to dismiss the Character Picker Palette from code when I'd like to.
I have tried iterating through all open windows, and closing all but the main window like this, but although my print statement reports 2 other windows, it does not close the character picker palette :
let openwindows = NSApplication.shared.windows
for win in openwindows {
if win == self.view.window {
print("Found and ruled out the main window") // successfull
}else{
print(win.className)
// finds two other windows and prints
// _NSCandidateBarCorrectionPanel
// NSCorrectionSubPanel
//
win.close() // Has no effect
// win.orderOut(self) // Has no effect
// win.performClose(nil) // sounds system beep
}
}

Related

How to get character entered on Keydown Event in WPF C#

i'm using wpf c# in visual studio
i want to prevent user can enter Arabic character , Just Persian Character
like when user entered this value on keyboard → "ي" change it to "ی"
my means something like this :
when user press button to type "A" on keyboard i want to change this character, first check if is "A" change to "B"
i did it in Windows Form Application , but that code does not work in WPF
My Code in Windows From :
if (e.KeyChar.ToString() == "ي")
{
e.KeyChar = Convert.ToChar("ی");
}
My Code in WPF :
if (e.Key.ToString() == "ي")
{
e.Key.ToString("ی");
}
These codes not working in WPF
Please Help
It's a bit different in WPF.
This works using a english keyboard. Don't know if it will work with Arabic, as the rules might be a little different for inserting characters.
You can try handling the PreviewTextInput event of the TextBox.
XAML:
<TextBox PreviewTextInput="TextBox_OnTextInput" ...
Code:
private void TextBox_OnTextInput(object sender, TextCompositionEventArgs e)
{
var box = (sender as TextBox);
var text = box.Text;
var caret = box.CaretIndex;
if (e.TextComposition.Text == "ي")
{
var newValue = "ی";
//Update the TextBox' text..
box.Text = text.Insert(caret, newValue);
//..move the caret accordingly..
box.CaretIndex = caret + newValue.Length;
//..and make sure the keystroke isn't handled again by the TextBox itself:
e.Handled = true;
}
}

Swift 5.1 console app in Linux : keyboard arrow Up/Down behaviour

I'd like to add bash-like behaviour for my console app - when I press Arrow Up/Down I can see the previous commands entered. Here is the way I handle user input :
while true {
print(">> ", terminator : "")
if let inputCli = readLine() {
let lowerCasedInput = inputCli.lowercased()
if lowerCasedInput == "exit" {
break
}
}
}
What is the way to achieve this?
If I press Up and Down I see the following output :
^[[A^[[B
How can I handle these press events?

Maintain screen position always centered on cursor

I'm trying to write an extension for VSCode where the editor screen is always centered on the cursor. There are other extensions that add a command to center the screen onto the cursor, but you have to press the command to activate it.
Currently the only way I've found to implement this is to rewrite the cursorUp, cursorDown, enter, pageUp, pageDown -- any command that moves the cursor up and down basically, and then use the "revealLine" command with the cursor line position and with the "at" attribute as "center".
Is there a better way? Reimplementing the default editor commands seems very inefficient.
Here's what I've got currently:
"use strict";
import * as vscode from "vscode";
export function activate(context: vscode.ExtensionContext) {
let disposable1 = vscode.commands.registerCommand("cursorUp",() => {
centralizar();
vscode.commands.executeCommand("cursorMove", {
to: "up",
});
}
);
let disposable2 = vscode.commands.registerCommand("cursorDown",() => {
centralizar();
vscode.commands.executeCommand("cursorMove", {
to: "down",
});
}
);
context.subscriptions.push(disposable1);
context.subscriptions.push(disposable2);
}
function centralizar() {
let currentLineNumber = vscode.window.activeTextEditor.selection.start.line;
vscode.commands.executeCommand("revealLine", {
lineNumber: currentLineNumber,
at: "center"
});
}
export function deactivate() {}
Perhaps you can utilize the new (v1.38) command editor.cursorSurroundingLines. See scrollOff release notes.
It takes a number, but if you can get the number of visible lines in an editor window, then set editor.cursorSurroundingLines to the appropriate midpoint that may work. It seems you would not have to listen for scroll events. But would have to update the value if the editor window was resized.
The Scrolloff extension implements this functionality when its "alwaysCenter" option is enabled. It works by listening to window.onDidChangeTextEditorSelection, which fires when the cursor moves.

How to create a custom dialog in VSCode?

I'm developing an extension for VSCode, and I want to display a custom dialog to help the user configure an ini file.
Is it possible to create a custom dialog with labels and inputs?
You cannot create new UI elements, but if you want to get inputs from the user you can use code like below:
let options: InputBoxOptions = {
prompt: "Label: ",
placeHolder: "(placeholder)"
}
window.showInputBox(options).then(value => {
if (!value) return;
answer1 = value;
// show the next dialog, etc.
});
This will use the same UI as the command palette (when you press Ctrl+P, or any of the other commands that open the input box at the top).

makeFirstResponder: does not always take cursor

When I display my app with a keyboard shortcut or by tapping an icon in the status bar I set first responder. makeFirstResponder: always succeeds (returns true) with a not-nil window and a not-nil NSTextField. However it doesn't always 'take the cursor' (i.e. move the flashing | to the NSTextField).
For example,
display app - takes cursor ✓
tap outside app
display app again - does not take cursor (even though makeFirstResponder returns true).
Here's how I'm trying to do this:
//Find the key window. I don't think this is the problem because self.window produces the same results.
var keyWindow:NSWindow = popover.contentViewController!.view.window!
for window in NSApplication.sharedApplication().windows{
if (window.keyWindow){
Swift.print("window \(window) is the key window")
keyWindow = window
}
}
//iFR is a variable that I use to keep track of which NSTextField I want to focus on. It's always a valid textField and most of the time (but not always) it's the only NSTextField in the view.
if (initialFirstResponder != nil)
{
if keyWindow.makeFirstResponder(initialFirstResponder)
{
Swift.print("first responder success")
}else
{
//Never happens:
Swift.print("first responder FAIL")
}
}else
{
Swift.print("no initial firstResponder")
}
I feel like I'm bludgeoning the responder chain into submission but,
NSApp.activateIgnoringOtherApps(true)
causes the cursor to always be captured (and asked nicely) to go where I want it to.