Skip to content

Commit

Permalink
working shortcuts for most of the buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-chwedczuk committed Nov 21, 2024
1 parent 794baff commit 29addf9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 35 deletions.
1 change: 1 addition & 0 deletions gui/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
requires javafx.web;
requires vaadin.sass.compiler;
requires sac;
requires jsr305;


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
import javafx.beans.binding.ObjectExpression;
import javafx.beans.binding.StringExpression;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
import javafx.scene.input.KeyCode;
import javafx.scene.text.Font;
import mscalc.engine.*;
import mscalc.engine.commands.Command;
import mscalc.engine.commands.IExpressionCommand;
import mscalc.engine.resource.JavaBundleResourceProvider;
import mscalc.gui.views.scientific.ScientificView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import java.util.*;

public class ScientificCalculatorViewModel {
private static final Logger logger = LogManager.getLogger(ScientificCalculatorViewModel.class);

private final List<InputViewModel> allInputs = new ArrayList<>();

private final CalculatorManager calculatorManager = new CalculatorManager(
new ThisViewModelCalculatorDisplay(),
new JavaBundleResourceProvider());
Expand All @@ -38,6 +33,8 @@ public class ScientificCalculatorViewModel {
public final ObjectProperty<NumberWidth> integerNumberWidthProperty = new SimpleObjectProperty<>(NumberWidth.QWORD_WIDTH);
public final ObjectProperty<DegreeType> degreeTypeProperty = new SimpleObjectProperty<>(DegreeType.Degrees);

// TODO: Model enum as 4 buttons with InputViewModel

public final StringProperty displayProperty = new SimpleStringProperty("");

// --- CONTROL BUTTONS ---
Expand Down Expand Up @@ -195,7 +192,7 @@ public class ScientificCalculatorViewModel {
// -- ARITHMETIC FUNCTIONS ---

public final InputViewModel divideButton = newInputViewModel()
.withText("/")
.withText("÷")
.withKeyboardShortcut(KeyCode.SLASH)
.withCommand(Command.CommandDIV)
.build();
Expand Down Expand Up @@ -408,7 +405,15 @@ public record KeyboardCode(
KeyCode key,
boolean control,
boolean shift
) { }
) {
public String toShortcutDescription() {
return String.format("Keyboard shortcut: %s%s%s",
shift ? "Shift+" : "",
control ? "Ctrl+" : "",
Character.isISOControl(key.getChar().charAt(0))
? key.getName() : key.getChar());
}
}

public class InputViewModelBuilder {
private StringExpression textProperty;
Expand Down Expand Up @@ -497,9 +502,14 @@ public InputViewModelBuilder withEnabled(BooleanExpression enabledProperty) {
}

public InputViewModel build() {
var ivm = new InputViewModel(textProperty, tooltipProperty, commandProperty, enabledProperty);
allInputs.add(ivm);
return ivm;
if (keyboardShortcut != null) {
tooltipProperty = new ReadOnlyStringWrapper(
((tooltipProperty.get() != null) ? (tooltipProperty.get() + "\n\n") : "") +
keyboardShortcut.toShortcutDescription());
}

return new InputViewModel(textProperty, tooltipProperty,
commandProperty, enabledProperty, keyboardShortcut);
}
}

Expand All @@ -508,16 +518,19 @@ public class InputViewModel {
private final StringExpression tooltipProperty;
private final ObjectExpression<Command> commandProperty;
private final BooleanExpression enabledProperty;
private final KeyboardCode keyboardShortcut;

public InputViewModel(StringExpression textProperty,
StringExpression tooltipProperty,
ObjectExpression<Command> commandProperty,
BooleanExpression enabledProperty) {
BooleanExpression enabledProperty,
@Nullable KeyboardCode keyboardShortcut) {

this.textProperty = Objects.requireNonNull(textProperty);
this.tooltipProperty = Objects.requireNonNull(tooltipProperty);
this.commandProperty = Objects.requireNonNull(commandProperty);
this.enabledProperty = Objects.requireNonNull(enabledProperty);
this.keyboardShortcut = keyboardShortcut;
}

public void execute() {
Expand All @@ -539,9 +552,11 @@ public ObjectExpression<Command> commandProperty() {
public BooleanExpression enabledProperty() {
return this.enabledProperty;
}
}

// TODO: Add logger
public Optional<KeyboardCode> keyboardShortcut() {
return Optional.ofNullable(keyboardShortcut);
}
}

public class ThisViewModelCalculatorDisplay implements CalcDisplay {
private static final Logger logger = LogManager.getLogger(ThisViewModelCalculatorDisplay.class);
Expand Down
56 changes: 37 additions & 19 deletions gui/src/main/java/mscalc/gui/views/scientific/ScientificView.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import mscalc.engine.RadixType;
import mscalc.gui.App;
import mscalc.gui.viewmodel.ScientificCalculatorViewModel;
import mscalc.gui.viewmodel.ScientificCalculatorViewModel.KeyboardCode;
import mscalc.gui.views.CalculatorView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.HashMap;

public class ScientificView extends VBox implements CalculatorView {
private static final Logger logger = LogManager.getLogger(ScientificView.class);

private final ScientificCalculatorViewModel viewModel = new ScientificCalculatorViewModel();
private final HashMap<KeyboardCode, Button> keyboardMapping = new HashMap<>();

public ScientificView() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ScientificView.fxml"));
Expand Down Expand Up @@ -334,6 +338,20 @@ public void uninstall() {
private void bindButton(Button button, ScientificCalculatorViewModel.InputViewModel operation) {
button.textProperty().bind(operation.textProperty());

var defaultFont = button.getFont();
button.fontProperty().bind(button.textProperty().map(t -> {
if (t == null) return defaultFont;
if (t.length() < 2) {
// Increase font size
return new Font(defaultFont.getName(), defaultFont.getSize() + 1);
} else if (t.length() > 3) {
// Decrease font size
return new Font(defaultFont.getName(), defaultFont.getSize() - 1);
} else {
return defaultFont;
}
}));

// Pre-create the tooltip object
Tooltip t = new Tooltip();
t.textProperty().bind(operation.tooltipProperty());
Expand All @@ -348,35 +366,35 @@ private void bindButton(Button button, ScientificCalculatorViewModel.InputViewMo

button.disableProperty().bind(operation.enabledProperty().not());
button.setOnAction(e -> operation.execute());

button.setUserData(operation);

operation.keyboardShortcut().ifPresent(ks -> {
var conflict = keyboardMapping.put(ks, button);
if (conflict != null) {
logger.error("Conflicting keyboard mapping: {}", ks);
}
});
}

private void onKeyPressed(KeyEvent e) {
switch (e.getCode()) {
case KeyCode.S -> {
bSine.arm();
}
KeyboardCode kc = new KeyboardCode(e.getCode(), e.isControlDown(), e.isShiftDown());

default -> {
return;
}
if (keyboardMapping.containsKey(kc)) {
keyboardMapping.get(kc).arm();
e.consume();
}

e.consume();
}

private void onKeyReleased(KeyEvent e) {
switch (e.getCode()) {
case KeyCode.S -> {
bSine.disarm();
bSine.fire();
}
KeyboardCode kc = new KeyboardCode(e.getCode(), e.isControlDown(), e.isShiftDown());

default -> {
return;
}
if (keyboardMapping.containsKey(kc)) {
var button = keyboardMapping.get(kc);
button.disarm();
button.fire();
e.consume();
}

e.consume();
}

}
Binary file added gui/src/main/resources/beep.wav
Binary file not shown.

0 comments on commit 29addf9

Please sign in to comment.