-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from fyne-io/feature/printscreen
Feature/printscreen
- Loading branch information
Showing
12 changed files
with
208 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ui | ||
|
||
import ( | ||
"image" | ||
"image/png" | ||
|
||
"fyne.io/fyne" | ||
"fyne.io/fyne/canvas" | ||
"fyne.io/fyne/container" | ||
"fyne.io/fyne/dialog" | ||
"fyne.io/fyne/layout" | ||
"fyne.io/fyne/storage" | ||
"fyne.io/fyne/widget" | ||
) | ||
|
||
func (l *desktop) screenshot() { | ||
bg := l.wm.Capture() | ||
l.showCaptureSave(bg) | ||
} | ||
|
||
func (l *desktop) screenshotWindow() { | ||
win := l.wm.TopWindow() | ||
if win == nil { | ||
fyne.LogError("Unable to print window with no window visible", nil) | ||
return | ||
} | ||
|
||
img := win.Capture() | ||
if img == nil { | ||
return | ||
} | ||
l.showCaptureSave(img) | ||
} | ||
|
||
func (l *desktop) showCaptureSave(img image.Image) { | ||
w := fyne.CurrentApp().NewWindow("Screenshot") | ||
save := widget.NewButton("Save...", func() { | ||
saveImage(img, w) | ||
}) | ||
save.Importance = widget.HighImportance | ||
buttons := container.NewHBox( | ||
layout.NewSpacer(), | ||
widget.NewButton("Cancel", func() { | ||
w.Close() | ||
}), | ||
save) | ||
|
||
preview := canvas.NewImageFromImage(img) | ||
preview.FillMode = canvas.ImageFillContain | ||
w.SetContent(container.NewBorder(nil, buttons, nil, nil, preview)) | ||
w.Resize(fyne.NewSize(480, 360)) | ||
w.Show() | ||
} | ||
|
||
func saveImage(pix image.Image, w fyne.Window) { | ||
d := dialog.NewFileSave(func(write fyne.URIWriteCloser, err error) { | ||
if write == nil { // cancelled | ||
return | ||
} | ||
if err != nil { | ||
dialog.ShowError(err, w) | ||
} | ||
|
||
err = png.Encode(write, pix) | ||
if err != nil { | ||
dialog.ShowError(err, w) | ||
} | ||
|
||
w.Close() | ||
}, w) | ||
d.SetFilter(storage.NewMimeTypeFileFilter([]string{"image/png"})) | ||
d.Show() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// +build linux openbsd freebsd netbsd | ||
|
||
package x11 | ||
|
||
import ( | ||
"image" | ||
"math" | ||
|
||
"fyne.io/fyne" | ||
"github.com/BurntSushi/xgb" | ||
"github.com/BurntSushi/xgb/xproto" | ||
) | ||
|
||
// CaptureWindow allows x11 code to get the image representation of a screen area. | ||
// The window specified will be captured according to its bounds. | ||
func CaptureWindow(conn *xgb.Conn, win xproto.Window) *image.NRGBA { | ||
geom, err := xproto.GetGeometry(conn, xproto.Drawable(win)).Reply() | ||
if err != nil { | ||
fyne.LogError("Unable to get screen geometry", err) | ||
return nil | ||
} | ||
pix, err := xproto.GetImage(conn, xproto.ImageFormatZPixmap, xproto.Drawable(win), | ||
0, 0, geom.Width, geom.Height, math.MaxUint32).Reply() | ||
if err != nil { | ||
fyne.LogError("Error capturing window content", err) | ||
return nil | ||
} | ||
|
||
img := image.NewNRGBA(image.Rect(0, 0, int(geom.Width), int(geom.Height))) | ||
i := 0 | ||
for y := 0; y < int(geom.Height); y++ { | ||
for x := 0; x < int(geom.Width); x++ { | ||
copyPixel(pix.Data, img.Pix, i) | ||
i += 4 | ||
} | ||
} | ||
return img | ||
} | ||
|
||
func copyPixel(in []byte, out []uint8, i int) { | ||
b := in[i] | ||
g := in[i+1] | ||
r := in[i+2] | ||
out[i] = r | ||
out[i+1] = g | ||
out[i+2] = b | ||
out[i+3] = 0xff // some colour maps / depths don't include alpha | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// +build linux openbsd freebsd netbsd | ||
|
||
package x11 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCopyPixel(t *testing.T) { | ||
in := []byte{0xff, 0x99, 0x33, 0xff} | ||
out := []uint8{0, 0, 0, 0} | ||
copyPixel(in, out, 0) | ||
|
||
assert.Equal(t, uint8(0x33), out[0]) | ||
assert.Equal(t, uint8(0x99), out[1]) | ||
assert.Equal(t, uint8(0xff), out[2]) | ||
assert.Equal(t, uint8(0xff), out[3]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters