Skip to content

Commit

Permalink
Fixing key pressing
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Sep 30, 2022
1 parent c75b9a0 commit 2c81b2d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 49 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The LUWRAIN Project, please visit http:https://luwrain.org/doc/authors/ to get a complete list of our developers
The LUWRAIN Project, please visit https:https://luwrain.org/doc/authors/ to get a complete list of our developers
39 changes: 17 additions & 22 deletions src/main/java/org/luwrain/app/viewer/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.luwrain.app.base.*;
import org.luwrain.util.*;

import static org.luwrain.util.UrlUtils.*;

public class App extends AppBase<Strings>
{
static final String
Expand All @@ -45,37 +47,30 @@ public App(String arg)

@Override protected AreaLayout onAppInit() throws IOException
{
final File file;
if (arg != null && !arg.isEmpty())
load(arg);
final ViewPdf viewPdf = createPdfView();
viewPdf.show();
file = urlToFile(arg); else
file = null;
if (file != null && file.getName().toUpperCase().endsWith(".PDF"))
{
final ViewPdf viewPdf = createPdfView(file);
viewPdf.show();
setAppName(file.getName());
} else
setAppName(getStrings().appName());
this.mainLayout = new MainLayout(this);
return mainLayout.getAreaLayout();
}

private void load(String file)
@Override public boolean onEscape()
{
/*
NullCheck.notEmpty(file, "file");
try {
this.url = new URL(file);
}
catch(Exception e)
{
this.text = new String[]{
"",
"ERROR:",
e.getClass().getName(),
e.getMessage(),
};
luwrain.onAreaNewContent(area);
}
*/
closeApp();
return true;
}

private ViewPdf createPdfView() throws IOException
private ViewPdf createPdfView(File file) throws IOException
{
return new ViewPdf(getLuwrain()){
return new ViewPdf(getLuwrain(), file){
@Override void inaccessible() { getLuwrain().playSound(Sounds.ERROR); }
@Override void announcePage(int pageNum, int pageCount) { message("Страница " + pageNum, Luwrain.MessageType.OK); }
@Override void announceMoveLeft() {}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/luwrain/app/viewer/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.luwrain.core.*;
import org.luwrain.cpanel.*;
import org.luwrain.i18n.*;

public final class Extension extends EmptyExtension
{
Expand All @@ -31,12 +32,10 @@ public final class Extension extends EmptyExtension
@Override public ExtensionObject[] getExtObjects(Luwrain luwrain)
{
return new ExtensionObject[]{

new Shortcut() {
@Override public String getExtObjName() { return "viewer"; }
@Override public Application[] prepareApp(String[] args)
{
NullCheck.notNullItems(args, "args");
if (args.length == 0)
return new Application[]{new App()};
final List<Application> v = new ArrayList();
Expand All @@ -47,7 +46,20 @@ public final class Extension extends EmptyExtension
return v.toArray(new Application[v.size()]);
}
},

};
}

@Override public void i18nExtension(Luwrain luwrain, org.luwrain.i18n.I18nExtension i18nExt)
{
i18nExt.addCommandTitle(Lang.EN, "viewer", "Graphical preview");
i18nExt.addCommandTitle(Lang.RU, "preview", "Графический просмотр");
try {
i18nExt.addStrings(Lang.EN, Strings.NAME, new ResourceStringsObj(luwrain, getClass().getClassLoader(), getClass(), "strings.properties").create(Lang.EN, Strings.class));
i18nExt.addStrings(Lang.RU, Strings.NAME, new ResourceStringsObj(luwrain, getClass().getClassLoader(), getClass(), "strings.properties").create(Lang.RU, Strings.class));
}
catch(java.io.IOException e)
{
throw new RuntimeException(e);
}
}
}
42 changes: 19 additions & 23 deletions src/main/java/org/luwrain/app/viewer/ViewPdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,37 @@ abstract class ViewPdf
private double offsetX = 0;
private double offsetY = 0;

ViewPdf(Luwrain luwrain) throws IOException
ViewPdf(Luwrain luwrain, File file) throws IOException
{
this.luwrain = luwrain;
this.doc = PDDocument.load(new File("/tmp/proba.pdf"));
this.doc = PDDocument.load(file);
this.rend = new PDFRenderer(doc);
Log.debug(LOG_COMPONENT, "PDF renderer created");
}

abstract void inaccessible();
abstract void inaccessible();
abstract void announcePage(int pageNum, int pageCount);
abstract void announceMoveLeft();
abstract void announceMoveRight();
abstract void announceMoveRight();
abstract void announceMoveUp();
abstract void announceMoveDown();
abstract void announceMoveDown();
abstract void announceZoomIn();
abstract void announceZoomOut();

void show()
{
luwrain.showGraphical((control)->{
try {
this.canvas = new ResizableCanvas();
this.canvas.setOnKeyPressed((event)->onKey(event));
this.canvas.setVisible(true);
luwrain.showGraphical((graphicalModeControl)->{
try {
this.canvas = new ResizableCanvas();
this.canvas.setOnKeyPressed((event)->onKey(graphicalModeControl, event));
this.canvas.setVisible(true);
this.canvas.requestFocus();
drawInitial();
return canvas;
}
catch(Throwable e)
{
throw new RuntimeException(e);
}
drawInitial();
return canvas;
}
catch(Throwable e)
{
throw new RuntimeException(e);
}
});
}

Expand Down Expand Up @@ -169,10 +168,8 @@ private void drawInitial()
{
this.offsetX = 0;
this.offsetY = 0;
Log.debug(LOG_COMPONENT, "canvas " + String.format("%.2f", canvas.getWidth()) + "x" + String.format("%.2f", canvas.getHeight()));
this.image = makeImage(pageNum, 1);
this.scale = (float)matchingScale(image.getWidth(), image.getHeight(), canvas.getWidth(), canvas.getHeight());
Log.debug(LOG_COMPONENT, "initial scale is " + String.format("%.2f", scale));
this.image = makeImage(pageNum, scale);
draw();
}
Expand Down Expand Up @@ -298,14 +295,13 @@ private Image makeImage(int pageNum, float scale)
return image;
}

private void onKey(KeyEvent event)
private void onKey(Interaction.GraphicalModeControl graphicalModeControl, KeyEvent event)
{
NullCheck.notNull(event, "event");
FxThread.ensure();
switch(event.getCode())
{
case ESCAPE:
close();
graphicalModeControl.close();
break;
case PAGE_DOWN:
// listener.onInputEvent(new InputEvent(InputEvent.Special.PAGE_DOWN));
Expand Down

0 comments on commit 2c81b2d

Please sign in to comment.