class_name SheetExportMenu extends RefCounted ## The sheet editor's "Export ▾" menu (Play and Build): a share file others ## can import (SheetFile), a web page to print or save as PDF (one page per ## tab, SheetExport.sheet_html), and a PNG of the tab as you see it. const ITEMS := ["Share file…", "Print / PDF (web page)…", "Image (PNG)…"] static func make_button(ed: SheetEditor) -> MenuButton: var button := Style.menu_button("Export ▾") button.name = "Export" button.tooltip_text = "Save this sheet as a file to share, print or show" button.get_popup().about_to_popup.connect(func() -> void: Actions.fill(button.get_popup(), actions(ed))) return button static func actions(ed: SheetEditor) -> Array: return [ {"text": ITEMS[0], "do": share.bind(ed), "tip": "A file others can import into their Sheets"}, {"text": ITEMS[1], "do": print_page.bind(ed), "tip": "Open it in a browser and print it, or save it as PDF. One page per tab."}, {"text": ITEMS[2], "do": image.bind(ed), "tip": "This tab, as you see it"}, ] static func share(ed: SheetEditor) -> void: ed.save_now() ed.m.layout.export_dialog("Share sheet", SheetFile.FILTER, SheetFile.file_name(ed.doc), func() -> String: return SheetFile.share_text(ed.doc)) static func print_page(ed: SheetEditor) -> void: ed.m.layout.export_dialog("Save a page to print", "*.html ; Web page", LibraryStore.slugify(ed.doc.name) + ".html", func() -> String: return page_html(ed)) static func page_html(ed: SheetEditor) -> String: return SheetExport.sheet_html(ed.doc, ed.m.sheet_links.expand) ## Waits for the menu to close and the canvas to draw, takes the picture, ## then asks where to save it. static func image(ed: SheetEditor) -> void: await RenderingServer.frame_post_draw await RenderingServer.frame_post_draw var picture := capture(ed.canvas) if picture == null: ed.m.toast("Couldn't take a picture of the sheet") return var file_name := "%s-%s.png" % [LibraryStore.slugify(ed.doc.name), LibraryStore.slugify(str(ed.current_tab().get("name", "tab")))] ed.m.layout.file_dialog("Save a picture of this tab", ["*.png ; PNG picture"], FileDialog.FILE_MODE_SAVE_FILE, func(path: String) -> void: var err := picture.save_png(path) ed.m.toast("Exported to %s" % path.get_file() if err == OK else "Couldn't write %s" % path.get_file()) , file_name) ## The part of the window that shows `canvas`, as an Image (null when ## nothing is drawn, as in headless runs). static func capture(canvas: Control) -> Image: if DisplayServer.get_name() == "headless": return null var shown := canvas.get_viewport().get_texture().get_image() if shown == null or shown.is_empty(): return null var to_pixels := canvas.get_viewport().get_final_transform() * canvas.get_global_transform_with_canvas() var region := Rect2i(to_pixels * Rect2(Vector2.ZERO, canvas.size)).intersection(Rect2i(Vector2i.ZERO, shown.get_size())) if region.size.x <= 0 or region.size.y <= 0: return null return shown.get_region(region)